开发者

How to listen for a checkbox in a listview row?

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.LENG开发者_如何学JAVATH_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.


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());


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜