Reputation: 157
I have a listview with checkboxes that has type CHOICE_MODE_NONE, because I want to make each item tri-state. It works fine using setChecked in onItemClick, to check and uncheck the items as required.
But when starting up the view, I want to set some items. The problem is that setItemChecked is only valid if CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE. So how do I check an item?
I tried the following, but ck is null:
int totalItems = getListView().getCount();
if (totalItems > 0)
{
for (int position=0; position<totalItems; position++)
{
CheckedTextView ck = (CheckedTextView)(lv.getChildAt(position));
ck.setChecked(true);
}
}
what am I doing wrong?
Upvotes: 2
Views: 312
Reputation: 157
got it, thanks to http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/ - overriding the arrayadapter gave me access to the view of each item on the list.
So I was able to write:
@Override
public View getView(int position, View v, ViewGroup parent)
{
if (v == null)
{
v = super.getView(position, v, parent);
}
CheckedTextView tv = (CheckedTextView) v;
tv.setChecked(passengers.get(position).isPassengerOnBoard());
return v;
}
Upvotes: 1