0

I have implemented custom list view with two rows (name and number) and it is checkable. The list view has multiple select option.

Whenever user searches for a name, cursor will returned the new list of items. I can't keep track for items which has been selected earlier once adapter gets changed with the new cursor items.

For example user searches for name "Jo" it returns 10 items, in which i have selected 2 rows. Once i remove the search, the cursor and adapter gets changed. I am not able to mark the items checked.

I want to override default checkable items based on position( have to write own which has to make items checkable based on _id(contact id)) ( I tried overriding onFinishInflate method. But it didn't help).

Any help appreciated. Thanks in advance.

swastican
  • 257
  • 1
  • 2
  • 15
  • "I can't keep track for items which has been selected earlier once adapter gets changed with the new cursor items." Sure you can... just key off of something from the source of your listview. – logray Dec 22 '12 at 03:53

1 Answers1

3

what you need is an object to have your check-box data persist your adapter and listview. A hashmap of boolean arrays should suffice.

private HashMap<String, boolean[]> contactMap;

I'd imagine that you could load this in some database method or something and you could have the person's name, like "Jo", as an identifier if need be. the array indexes would correspond to the checkboxes in each listview row as they appear. Then in your adapter which i'd imagine is a CursorAdapter, you could have the following:

private boolean[] contactObj;

public void setContactObj(boolean[] contactObj) {
    this.contactObj = contactObj;
    notifyDataSetChanged();
}

public boolean[] getContactObj() {
    return contactObj;
}

@Override
public void bindView(View view, Context context, Cursor c) {
    final int position = c.getPosition();
    final CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);

    cb.setChecked(contactObj[position]);
    cb.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (cb.isChecked()) {
                contactObj[position] = true;
            } else {
                contactObj[position] = false ;
            }
        }
    });
}

Basically, you have an adapter that only has capacity for one boolean[] which is able to adjust the checkboxes in your listview, modify as the boolean[] as the checkbox is being clicked and then return it in the event that you still need it.

mango
  • 5,577
  • 4
  • 29
  • 41
  • thanks mango. i fixed the above problem. Now i am facing another problem. See the below link http://stackoverflow.com/questions/14000837/checkable-displaying-wrong-items-selected-used-simplecursor-custom-layout – swastican Dec 23 '12 at 09:30
  • I have the problem in maintaining the checkable items, when adapter gets changed. (Some way i have create custom adapter which should track the items items based on my custom id[_contact_id]). the checked items are maintained, but it has been maintained by its position. So whenever no of listitems gets changed, adapter/checkable still keeps tracking the selected id based on its position. So it is selecting the wrong items see the above comment and the link. – swastican Dec 23 '12 at 09:44