Listview with TextView and Button. RowId of the clicked button

牧云@^-^@ 提交于 2019-12-01 22:39:48

Just create a private class inside you adapter like this and set listener on button in bindView method its will be like

public void bindView(View view, Context context, Cursor cur) {  
    this.mCursor = cur;

    TextView tvText1 = (TextView)view.findViewById(R.id.text1);            
    Button btnButtonOFF = (Button)view.findViewById(R.id.buttonOFF);

    tvText1.setText(cur.getString(cur.getColumnIndex(NotesDbAdapter.KEY_TITLE)));

    int idRow = cur.getColumnIndex(NotesDbAdapter.KEY_ROWID);
    btnButtonOFF.setOnClickListener(new OnItemClickListener(cur.getInt(idRow)));           

}

private class OnItemClickListener implements OnClickListener {
    private int position;


    public OnItemClickListener(int position) {
        super();
        this.position = position;
    }


    @Override
    public void onClick(View v) {
        // position is an id.
    }
}

here is code by which you can able to find out row position of button

override the method in ModuleCursorAdapter

 @Override
 public View getView(int position, View convertView, ViewGroup parent) 
{

    convertView= super.getView(position,convertView,parent );
     Button btnButtonOFF = (Button)convertView.findViewById(R.id.buttonOFF); 

    String tag =  btnButtonOFF.getTag().toString()+","+position;
     btnButtonOFF.setTag(tag);

}

now in onclick method of btnButtonOFFclicked listener get tag of View & extract the position attached with tag if helpful then please vote

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!