Reputation: 3054
I have a listview with a checkboxes in each row. I am using the viewholder pattern to load the list, like this:
View view = convertView;
if(view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.setting_list_item, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.cbCat = (CheckBox) view.findViewById(R.id.cbCat);
view.setTag(viewHolder);
}
ViewHolder vh = (ViewHolder) view.getTag();
vh.cbCat.setText(bt.getName());
But when I try to get the checked checkboxes, like this :
for(int i = lvCatList.getFirstVisiblePosition(); i < settingAdapter.getCount(); i++) {
View v = lvCatList.getChildAt(i);
CheckBox cbx = (CheckBox) v.findViewById(R.id.cbCat);
if(cbx != null && cbx.isChecked()) {
toast += " / " + settingAdapter.getItem(i).getName();
Log.d("CHECKED", toast);
}
Only the visible checkboxes can be get. And if the checkbox is not visible in the screen it throws exceptions.
How to check whether the chekboxes in the list are checked or not.?
}
Upvotes: 1
Views: 3846
Reputation: 1285
You can add selected checkbox value to arraylist when checking on checkbox
List<String> SelectedBox = new ArrayList<String>();
View.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if(isChecked)
{
SelectedBox.add(id);
}
else
{
SelectedBox.remove(id);
}
}
});
When you need to check whether the checkbox is checked just refer SelectedBox
Upvotes: 2
Reputation: 3117
Refer the following example to get the checked item from listview.ListView with checkbox
Upvotes: 2