I have a ListView
that uses a custom layout that has 5 child View
objects (4 TextView
and 1 CheckBox
) in the custom layout. Only the TextViews
are bound, however - the CheckBox
has no corresponding field in the database. The purpose of the CheckBox
is simply to identify which of the items being displayed I would like to process in "the next step". I'm using a custom ViewBinder
to assign the text values correctly (because some of the values from the DB are dates, etc).
Part of the user interface is three buttons - 'All', 'None', and 'Invert' that I use to toggle the status of each item in the list. For the 'All' button, for example, I do this with the following code (which I now know is NOT correct - I include it to show what I'm trying to do):
ListAdapter la = getListAdapter();
ListView lv = getListView();
int iCount = la.getCount();
for(int i=0; i<iCount; i++)
{
View vv = lv.getChildAt(i);
CheckBox cb = (CheckBox) vv.findViewById(R.id.ledgerItemCheckBox);
cb.setChecked(true);
}
This works fine as long as the number of items in the list doesn't exceed the list size so that all items are always visible. When I exceed that number, though, the 'getChildAt' function sometimes returns null because (if I understand correctly) it isn't meant to return all of the items in the list, only those items that are visible, which results in a NullPointerException
.
How can I properly set the CheckBox
on all views, even if they aren't visible?