How to listen for a checkbox in a listview row?

独自空忆成欢 提交于 2020-01-04 19:17:09

问题


String[] from = new String[] { CallrzDbAdapter.C_NAME,CallrzDbAdapter.C_EMAIL,CallrzDbAdapter.C_PHONE };
    int[] to = new int[] {R.id.cName, R.id.cEmail, R.id.cPhone };


    notes = new SimpleCursorAdapter(this,
            R.layout.row, cursor, from, to);

    ListView view =getListView();
    view.setHeaderDividersEnabled(true);
    view.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    view.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View view,
                int position, long arg3) {
            Toast.makeText(getApplicationContext(), "Hello"+position+" is clicked ",
                      Toast.LENGTH_SHORT).show();

            return false;
        }
      });

    //setListAdapter(notes);
    setListAdapter(notes);

I have a custom layout for the list row which has a checkbox as well. How can I create a listener for the checkbox event? I have searched and heard about bindView but is there anyone can explain it a bit more clearly? this is the link someone explained it but I couldnt plug it in my code.


回答1:


You can probably create your own ViewBinder and in setViewValue simply do something like:

 class MyViewBinder implements SimpleAdapter.ViewBinder {
  public boolean setViewValue(View view, Object data, String textRepresentation) {
   int id = view.getId();
   /* handle this particular item from our own view
   if (id == R.id.myViewId) {
    ((CheckBox) view).setOnItemLongClickListener(...);
    ((CheckBox) view).setText(...);
    return true;
   }
   return false;
  }
 }

You can thenjust use SampleAdapter for data and call

adapter.setViewBinder(new MyViewBinder());




回答2:


The view from onItemLongClick should contain your checkbox.

You can retreive it like you normally would:

Checkbox yourCheckbox = (Checkbox) view.findViewById(R.id.your_checkbox_id);

Correct me if i'm wrong, I normally use a Custom ArrayAdapter

EDIT:

You could look at this for an example. Android Series: Custom ListView items and adapters Hint: it's the getView in the example where you can findViewById your CheckBox



来源:https://stackoverflow.com/questions/7639779/how-to-listen-for-a-checkbox-in-a-listview-row

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