listview checkbox trouble in android

℡╲_俬逩灬. 提交于 2019-12-01 23:37:15

If you have CheckBoxes in ListView you have problem, because ListView reuses Views that aren't already visible on screen. That's why when you scroll down, it loads (a few times) that one CheckBox that was checked by you previously. So you can't rely on default CheckBox's behaviour. What you need is:

  1. Prevent user from checking CheckBox. You can do this by calling cb.setEnabled(false), where cb is CheckBox which you are using.

  2. Create your own adapter class that will extend SimpleCursorAdapter. In this class you will store list of indexes of items that are checked.

  3. Override getView() method in your adapter class and there manually set CheckBox to be checked if it's position is stored in checked items array.

  4. Create OnClickListener for your ListView which will take care of adding/removing checked items' positions from adapter's internal array.

Edit:

This would be the code of your adapter:

public class MySimpleCursorAdapter extends SimpleCursorAdapter {
    public ArrayList<Integer> mItemsChecked;

    public MySimpleCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);

        mItemsChecked = new ArrayList<Integer>();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        for(int i = 0; i < mItemsChecked.size(); i++) {
            if(mItemsChecked.get(i) == position) {
                CheckBox cb = (CheckBox)convertView.findViewById(R.id.checkBox1); // Instead of checkBox1, write your name of CheckBox 
                cb.setChecked(true);
            }
        }

        return super.getView(position, convertView, parent);
    }

    /* Just a handy method that will be responsible for adding/removing 
    items' positions from mItemsChecked list */ 
    public void itemClicked(int position) {
        for(int i = 0; i < mItemsChecked.size(); i++) {
            if(mItemsChecked.get(i) == position) {
                mItemsChecked.remove(i);
                return;
            }
        }
        mItemsChecked.add(position);
    }
}

This is OnItemClickListener for your ListView:

final ListView lv = (ListView)findViewById(R.id.listView1); // Your ListView name
lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        ((MySimpleCursorAdapter)lv.getAdapter()).itemClicked(position);
            lv.getAdapter().notifyDataSetChanged(); // Because we need to call getView() method for all visible items in ListView, so that they are updated
    }
});

Just remember to disable your CheckBox so that user can't click it.

Now you can see it as a little hack, because user doesn't click CheckBox at all - application sees it as clicking in specific ListView item, which in turn automatically checks appropriate CheckBox for you.

Here is simple example that will help you to implement that scenario

http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

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