how to get a list item position by clicking the button inside it?

前端 未结 8 497
余生分开走
余生分开走 2020-12-09 16:48

actually I\'ve read some previous questions about this...

this is the code that I use

auto = (ListView)findViewById(R.id.auto);
String[] projection =         


        
相关标签:
8条回答
  • 2020-12-09 17:44

    I think one more best approach is there.You can add one tag as position with the button using (button.setTag()) method and whenever user will click button just take the tag of this button. it will more easy.

    Thanks and Regards Rahul

    0 讨论(0)
  • 2020-12-09 17:45

    Very Simple. In your Adapter class getView method change int position to final int position.

    public View getView(final int position, View convertView, ViewGroup parent) {
    
    LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(yourcustomizeditemview.xml, parent,
                    false);
    Button btn= (Button) row.findViewById(R.id.btn);
    btn.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
    
                    Toast.makeText(context, " "+position, Toast.LENGTH_SHORT).show();
    
                }
            });
    return row;
        }
    

    now on Button click Toast shows the position of item in which Button is present.

    0 讨论(0)
提交回复
热议问题