开发者

how to capture the checkbox event in ListView

I have added only checkboxes in my ListView.

list_checkbox.xml:

<?xml version="1.0" encoding="utf-8"?>

<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

开发者_JS百科android:layout_height="42px">

</CheckBox>

In java; to capture event on list i have used this::

items1={" "," "," "," "}; //blank i.e. no text near checkbox

lv1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_checkbox, items1));
lv1.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

      // When clicked,put code here.....
    }
  });

but now where and how to capture the checkbox event? in detail plz.

i am a newbie.

thanx..


If you put in the ListView either focusable or clickable Views then the your OnItemClickListener won't get called. Checkbox is clickable by default so setting OnItemClickListener in this case won't do anything.

What you can do in this case is to implement a custom Adapter (just extend ArrayAdapter) and add an OnClickListener to every View you provide for the ListView.

public class TestAdapter extends ArrayAdapter {
    ...
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = super.getView(position, convertView, parent);
        v.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Do your logic here eg. if ((CheckBox)v).isChecked()...
             }
         });
         return v;
    }
}

You can find a very good tutorial on this topic here. It's worth reading the whole chapter but the answer to your question is on page 112, Interactive Rows.

Also look at this Stack Overflow question.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜