I have a ListView that has 649 entries. Each View in the list has two LinearLayouts, an ImageView, a few TextViews, and a CheckBox. I currently have code to go through all the CheckBoxes and count them, and more code to figure out which ones are checked so I can use them. However, whenever I check one of the CheckBoxes, every seventh CheckBox above and below it magically become checked as well. The method to count the checked boxes returns the number I actually checked, but the method to get the indices of all the checked boxes just returns the first x checked, regardless of whether I checked them or the ones 7 away from them. For example, if I check number 21, the method returns index 3. I have included some of the relevant code segments:
The layout code for each View in the list:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/id"
android:layout_width="50dp"
android:layout_height="50dp"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="180dp"
android:layout_height="50dp"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#777777"
android:id="@+id/type"/>
</LinearLayout>
<CheckBox
android:layout_gravity="right|center_horizontal"
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"/>
Check counting method:
public int[] whichAreChecked() //TODO: this should work.
{
int listItemCount = ChooserList.getChildCount();
ArrayList<Integer> ints=new ArrayList<Integer>();
for(int i=0; i<listItemCount; i++)
{
CheckBox cbox = (CheckBox) ((View)ChooserList.getChildAt(i)).findViewById(R.id.check);
if(cbox.isChecked())
ints.add(i);
}
Log.e("durr", ""+ints.size());
int[] result=new int[ints.size()];
for(int i=0; i<result.length; i++)
result[i]=ints.get(i);
return result;
}
Check counting method:
public int countChecks()
{
int checked=0;
int listItemCount = ChooserList.getChildCount();
for(int i=0; i<listItemCount; i++)
{
CheckBox cbox = (CheckBox) ((View)ChooserList.getChildAt(i)).findViewById(R.id.check);
if(cbox.isChecked())
checked++;
}
Log.e("countChecks()", ""+checked);
return checked;
}
If I can add any extra info, please ask!
Hmmmm I think you misunderstood what the
getChildCount()andgetChildAt()methods do. Those will actually get only the rendered views. Unless I misread something here, if you have 650ish rows you cannot accomplish what you want with the views that are being rendered. Instead, you should implement anonItemClickListener(or, alternatively, something that fires when the checkboxes are clicked) and after the checkbox is checked, save that state into your model.