Sorry for the silly question. I know how to get checked items from ListView (MultipleChoice) with a SparseBooleanArray. But how to get the unchecked items?
Handling the SparseBooleanArray is pretty simple once you get it. If you know which items are checked you should be able to know which items are not checked by making the assumption that all items that are not in the checked positions are unchecked.
SparseBooleanArray checkedPositions = list.getCheckedItemPositions();
for(int i=0; i<myList.size(); i++) {
if(checkedPositions.get(i)) {
// CHECKED
} else {
// NOT CHECKED
}
}
Related
I'm using listview with ArrayAdapter and have following code:
final SparseBooleanArray checked = listViewFilters.getCheckedItemPositions();
if (checked.size() == listViewFilters.getAdapter().getCount()) {
//all is selected
} else {
//collect each value
}
All worked fine until I needed all items to be selected from start. So I added this code for selecting all items:
for (int i = 0; i < listViewFilters.getAdapter().getCount(); i++) {
listViewFilters.setItemChecked(i, true);
}
After adding above code, if I checked off some items, checked.size() = count of all items anyway. Expected result is - checked.size() < count.
Thanks.
well. listView.getCheckedItemCount() works fine.
It's weird to see that listView.getCheckedItemCount() returns different value than listView.getCheckedItemPositions().size() only after setItemChecked(i, true)
getCheckedItemPositions might prove to be a little tricky. As you noticed, based on the interaction with the ListView, it might return different results.
Short version: once you selected and deselected an item, that item is still returned by getCheckedItemPositions, with a value of false. So you need to actually read the values from the keys instead of relying on the size.
Following the guide found here http://developer.android.com/guide/topics/ui/menus.html#CAB I went up to a dead end on how to remove all the selected items from the listView's adapter.
In the guide it is shown as a method called deleteSelectedItems(); but since it is never implemented, I got stuck. How can I do this?
I asume you are using a List. Do the following:
private void deleteSelectedItems() {
SparseBooleanArray checked = mListView.getCheckedItemPositions();+
List<YourObject> list = mListOfObjects;
for (int i = 0; i < mListView.getCount(); i++)
if (checked.get(i))
YourObject item = list.get(i);
mListOfObjects.remove(item); //or whatever you want to do with it.
}
I successfully implemented an AlertDialog that shows a custom-styled-row of CheckedTextViews. The problem comes when selecting the items. At the end, the order is going to be established by their position in the ListView. If a user selects an item from the bottom of the list and then an item from the top, the answer given by the Dialog's OnClickListener will begin with the top one instead from the bottom one. Is there a way to sort these items by click?
Here is my implementation to retrieve the items checked:
private void setCheckedItems(){
SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
if (checkedItems != null) {
for (int i = 0; i<checkedItems.size(); i++) {
if (checkedItems.valueAt(i)) {
mSelectedItems.add(tables[checkedItems.keyAt(i)]);
}
}
}
}
Note: I tried to avoid this problem by calling OnItemClickListener on my ListView, but the items that were in positions longer than the space of the screen began to be checked as well. So, at the end I used listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); instead of the click listener.
Thank you very much.
I have an Android application. In which I have ListView. List view is constructed by ArrayAdapter and layout file.
In layout file I have checkbox. So I want to see if all checkboxes are checked or not by pressing button. How I can do these?
Update: I want to see which checkboxes are checked
You can get the number of checked items in your list with the code:
private int getChecked()
{
SparseBooleanArray checked = yourList.getCheckedItemPositions();
return checked.size();
}
Edit:
To check that every item is checked:
private boolean isEveryChecked()
{
SparseBooleanArray checked = yourList.getCheckedItemPositions();
return checked.size()==yourList.getCount();
}
yourList is a ListView.
I am using a ListView with MULTIPLE_CHOICE and to get the selected items back i am using setItemChecked() method.
It works fine as i am able to see the previously checked items.
The issue is that if i uncheck one of the previously checked items, and then get the list of checked items by custList.getCheckItemIds()
the array still has the Item that i unchecked.
Can anyone please tell me if that is supposed to happen or am i missing something?
Here you have to call setOnCheckedChangeListener and you have to manage the code inside this listener block.
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Write and manage your code here.
}
});
If you are simply trying to find out what items are checked at any given time, you can get a SparseBooleanArray from the ListView, and iterate over it with a for loop. For example:
SparseBooleanArray checked = list.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++){
if (checked.get(i))
//the item at index i is checked, do something
else
//the item is not checked, do something else
}
this:
SparseBooleanArray checked = list.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++){
if (checked.get(i))
//the item at index i is checked, do something
else
//the item is not checked, do something else
}
doens't work.
follow Multiple Contact Picker List [getCheckedItemPositions()]
should be OK.
SparseBooleanArray selectedPositions = listView.getCheckedItemPositions();
for (int i=0; i<selectedPositions.size(); i++) {
if (selectedPositions.get(selectedPositions.keyAt(i)) == true) {
//do stuff
}
}