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

前端 未结 8 495
余生分开走
余生分开走 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:28

    An easier solution to this probably is when your Activity implements OnClickListener and you set the (casted) Context as OnClickListener to any view you want in the Adapter. For more robust code you can check with instanceof.

    public class MyActivity implements OnClickListener {
    
        // ...
    
    }
    
    public class MyAdapter extends CursorAdapter {
    
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
    
            final TextView tv = (View) view.findViewById(R.id.text1);
    
            setOnClickListener((OnClickListener)context);
    
            tv.setTag(cursor.getPosition());
    
        }
    
    }
    

    For convenience you can set the items position in the adapter as Tag of the view. That way you can easily lookup the whole item in the adapter.

提交回复
热议问题