Hello i’m working on an app where i’m basically just making a custom multi select listview i did something like this LeftAL is just an array list and this code below is in my onClick
if(LeftAL.contains(position)){
ImageView imageView = (ImageView) lView.getChildAt(position).findViewById(R.id.checkbox);
imageView.setImageResource(R.drawable.checkboxoff);
int i = position;
Integer intObj = new Integer(i);
LeftAL.remove(intObj);
}else{
ImageView imageView = (ImageView) lView.getChildAt(position).findViewById(R.id.checkbox);
imageView.setImageResource(R.drawable.checkbox);
LeftAL.add(position);
}
and then checkboxoff looks like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/checkbox_pressed_off" />
<item android:drawable="@drawable/checkbox_off" />
</selector>
and checkbox on is the same thing basically just different images
Now that all works fine as it should and i didn’t actually notice this for a little while but when i check say item 3 in my list. It checks and adds to the array list as it should however if i scroll down to the next group of list items i will see that number 3 of the second set is also checked for instance lets just say for example only 3 rows show on my phone.
Cat
Dog
Egg
-------off screen-----
Fox
Gerbil
Hog
I check Dog then i scroll gerbil would also be checked i guess this is just android like recycling or something but it’s definately an issue how can i fix this so that only 3 in the list checks not 3 in the next set as well.
Thank you for any help
Android absolutely recycles the
Views in aListView. What you need to do is ensure that you are storing the checked / unchecked state in a separate list and set the state of the check box accordingly each time it’s shown.