Getting an Issue While Checking the Dynamically Generated Checkbox Through List View

Getting an issue while checking the dynamically generated checkbox through list view

I edited my answer so the common information is located at the top. You'll find the actual answer to this question at the bottom...


Here's the actual idea and process of recycling so you might figure out what is wrong with your implementation and idea of getView (and maybe others too when they will find this question and answer). See further below for a code example, just ignore the type part since this is an additional information.

  • Phase 1: Item creation for recycling (convertView is null):

    This means that you create the layout and the common state which is shared by all items. If you have listeners you have add them here and design them that way that they can react on position changes (when it is reused) later on. So for example by setting the position as tag on the corresponding view so the listener can catch this information and know on which item it is currently operating. You can't use the views to store data. So when the listener change a state on a list item you should persist this data (in an data array, in a SQLite database, etc) and use it in phase 2.

  • Phase 2: Setup item state for given position:

    You set the visual state for the item. Everything which might change individually for an item (text, checkbox state, colors, etc) has to be set here. Not only what have changed for the current item but could have been changed by another item. This way you make sure that the view is not used in an invalid state because it's being reused from another list item before.


The accpeted answer was deleted / edited but was suggesting to implement getItemViewType and getViewTypeCount so every list item had its own view type. The edited answer shows now how to solve the problem the way it's described here.

Reimplementing getItemViewType and getViewTypeCount works but your obviously misinterpreting it's use (compare my example further below and/or this answer).

These two methods are there for using two (or more) list items which completely differs from each other (e.g. a common list item and a separator which contains a title only) and not to avoid recycling of a view which could be reused.

If you're using them anyway to solve your problem you probably didn't understand the process I explained before. So e.g. you have 1000 items and you do the view type hack then you're creating 1000 views (hierarchies) instead probably 10 which could be reused easily. That shouldn't matter that much if you have only 20 items or so but if you use that technique for big lists you're just wasting (precious) memory!

Here's an example:

void getItemViewType(int position) {
return isItemAtPositionSeperator(position) ? 1 : /* normal item */ 0;
}

void int getViewTypeCount() {
return 2; // normal item and separator
}

void View getView(int position, View convertView, ViewGroup parent) {
int type = getItemViewType(position);

// phase 1: see my explanation
if (convertView == null) {
if (type == 0) {
// setup your common item view - inflate it and set to convertView
} else {
// setup separator view - inflate it and set to convertView
}
}

// phase 2: see my explanation
if (type == 0) {
// set the state of the common item view based on the position
// rely on the fact that convertView contains the view hierarchy
// you created in convertView == null && type == 0
} else {
// set state of the separator based on the position
// rely on the fact that convertView contains the view hierarchy
// you created in convertView == null && type != 0 (else part)
}

return convertView;
}

Actual answer to question...

I know what the problem is but can't think of an elegant solution right now...

Your problem is that you set the click listener once with viewHolder.checkbox.setOnCheckedChangeListener when the view is created. So it is recycled / reused for items when you scroll and the click behavior applies to the wrong list item.

Try not to hard-code the position by using the outer final position. Try setting viewHolder.checkbox.setTag(position) before return and then use (Integer) buttonView.getTag() instead position+1. So your recycled view will keep the actual position.

When you click a checkbox you should persists the state somewhere else. Don't rely on the UI state for that (because it will be recycled). So call viewHolder.checkbox.setChecked(persistedState) before return.

I hope this makes sense and you get the idea... ;-)

What is the good way to create dynamic check boxes in list view and write click event in Android?

This Android ListView Checkbox Example - OnItemClickListener() and OnClickListener() might provide some idea for your problem.

How to manage list views Checkboxes Automatically checking?

Create your own BaseAdapter.

Keep in mind, that ALL views in your listview you see are temporary. They will be recycled when you'll scroll away. The reason is - you can have >9000 elements in your list. So, the way you create views must depend on some kind of (!) data.

Here is nince tutorial on how to create your own list.

Make X-th checkbox depend on X-th boolean in the list. A bit confusing first time I know, but this is the best way.

class MyAdapter extends BaseAdapter{

List<boolean> myCheckBoxes;

boolean getItem(int arg0){

return myCheckBoxes.get(arg0);

}

View getView(int arg0, View arg1, ViewGroup arg2){

...
...//See article
myView.setChecked(getItem(arg0));

...
return myView;

}

And in your activity

ListView myListView;
...
myListView.setAdapter(new MyAdapger(...));

checkbox issue in installed app in listview

You can refer to my answer here, what you can do is,store check box state in a boolean array then you wont face this issue.....

Android checkbox multiselected issue

listview with checkbox status changed when scoll

convertview may give you previously generated view so dont rely on it. just store your checkbox state's in one boolean array & change that accordingly depends on checkchangelistener callback here is code for you I just modified getView method of your code everything else keep as is

 public View getView(final int position, View convertView, ViewGroup parent) {

View hView = getLayoutInflater().inflate(R.layout.friendslist, null);

ImageView profile_pic = (ImageView) hView.findViewById(R.id.profile_pic);
TextView name = (TextView) hView.findViewById(R.id.name);

final CheckBox checkbox=(CheckBox)hView.findViewById(R.id.checkbox);

checkbox.setTag(new Integer(position));
profile_pic.setImageBitmap(Utility.model.getImage(friendid[position], pictures[position]));
name.setText(names[position]);

checkbox.setOnCheckedChangeListener(null);
if(checkBoxState[position])
checkbox.setChecked(true);
else
checkbox.setChecked(false);

checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Integer pos = (Integer)buttonView.getTag();
if(isChecked){

checkBoxState[pos.intValue()]=true;
}
else{
checkBoxState[pos.intValue()]=false;
Log.e("checked","unchecked");

}
}
});

return hView;
}


Related Topics



Leave a reply



Submit