Get number of selected items in android ListView - android

I have a multiselection ListView in my android app. What I need to know is how many items on that list I have selected.

Use getCheckedItemCount() of ListView. This gives you directly an int.

I found a solution.
getCheckedItemPositions() will create a SparceBooleanArray. So it is just to count the elements which are true in that array.
Example:
SparseBooleanArray positions = theListView.getCheckedItemPositions();
int counter = 0;
if (positions != null) {
int length = positions.size();
for (int i = 0; i < length; i++) {
if (positions.get(positions.keyAt(i))) {
counter++;
}
}
}

Instead of using keyAt followed by get, we can use valueAt directly.
SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
int count = 0;
for (int i = 0, ei = checkedItemPositions.size(); i < ei; i++) {
if (checkedItemPositions.valueAt(i)) {
count++;
}
}

Related

Retrieve values of checked list items

i was following an example i saw online to retrieve checked items
but in the example it only retrieves one value
int len = listview.Count;
SparseBooleanArray checkedItems = listview.CheckedItemPositions;
for (int i = 0; i < len; i++)
if (checkedItems.Get(i))
{
string item = list[i];
/* do whatever you want with the checked item */
txt.Text = item;
}
So how do i Retrieve all of the checked items values?
You need to create a data structure of some sort
List<string> checkedItems = new List<string>();
int len = listview.Count;
SparseBooleanArray checkedItems =
listview.CheckedItemPositions;
for (int i = 0; i < len; i++)
if (checkedItems.Get(i))
{
checkedItems.Add(list[i]);
}

Remove all ImageViews using Tag

how does one iterate through a view's children and remove all ImageViews with a Tag that StartsWith a particular string?
All examples I can find this iterator;
for (int pos = 0; pos < mat.getChildCount(); pos++)
{
Object tag = mat.getChildAt(pos).getTag();
if (tag != null)
{
String s = tag.toString();
if (s.startsWith("Z"))
{
mat.removeView(mat.getChildAt(pos));
}
}
}
do a test, then remove the object.
The issue is both 'pos' and getChildCount change throughout the process. If I want to remove the first item and then the 2nd item (which after the first remove is actually the first item) it won't work, as pos is now 1 (ie 2nd item).
thanks
There are a few options.
for (int pos = 0; pos < mat.getChildCount();) {
if (remove) {
mat.removeViewAt(pos);
continue;
}
// only increment if the element wasn't removed
pos++;
}
for (int pos = 0; pos < mat.getChildCount(); pos++) {
if (remove) {
mat.removeViewAt(pos);
// balance out the next increment
pos--;
}
}
// don't remove views until after iteration
List<View> removeViews = new ArrayList<>();
for (int pos = 0; pos < mat.getChildCount(); pos++) {
if (remove) {
removeViews.add(mat.getChildAt(pos));
}
}
for (View view : removeViews) {
mat.removeView(view);
}
// count in reverse
for (int pos = mat.getChildCount() - 1; pos >= 0; pos--) {
if (remove) {
mat.removeViewAt(pos);
}
}

Android :How to count no checked items of checkboxes in listview

Android :How to count no checked items of checkboxes in listview when using Simple cursor Adapter
Call getCheckedItemCount() on the ListView.
int cntChoice = listView.getCount();
String checked = "";
String unchecked = "";
SparseBooleanArray sparseBooleanArray = listView.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++)
{
if(sparseBooleanArray.get(i) == true)
{
// checked = listView.getItemAtPosition(i).toString() ;
checked= list1.get(i).toString();
Log.i("CheckedItem", checked);
}
else if(sparseBooleanArray.get(i) == false)
{
//unchecked= listView.getItemAtPosition(i).toString();
unchecked= list1.get(i).toString();
Log.i("uncheckedItem", unchecked);
}
}
Hope this will help you. For more reference.

whats the equivilent of getCheckedItemCount() for API level < 11?

I am using this method to check how many items on a list a checked and I get this error that this method is not available for any SDK older than 11.
What is the equivalent this in API level 8
The accepted answer didn't work for me (always returns 0), I had to use the following code:
public static int getCheckedItemCount(ListView listView)
{
if (Build.VERSION.SDK_INT >= 11) return listView.getCheckedItemCount();
else
{
int count = 0;
for (int i = listView.getCount() - 1; i >= 0; i--)
if (listView.isItemChecked(i)) count++;
return count;
}
}
getCheckedItemIds().length seems to do the trick
I'm using this code which I believe is efficient and works in all cases:
public int getCheckedItemCount() {
ListView listView = getListView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
return listView.getCheckedItemCount();
}
SparseBooleanArray checkedItems = listView.getCheckedItemPositions();
int count = 0;
for (int i = 0, size = checkedItems.size(); i < size; ++i) {
if (checkedItems.valueAt(i)) {
count++;
}
}
return count;
}
Please inform me if you find a case where it doesn't work.

get checked items from listview in android

I have a dynamic listview with one text and one checkbox per line.when i click a button.,i need to get all checked item names & Unchecked item names separately as arraylilst.How could i do that.Examples are much better..
I used..
SparseBooleanArray checked = mainlw.getCheckedItemPositions();
for (int i = 0; i < checked.size(); i++) {
if(checked.valueAt(i) == true) {
Planet tag = (Planet) mainlw.getItemAtPosition(checked.keyAt(i));
String selectedName=tag.getName();
Toast.makeText(getApplicationContext(), selectedName, Toast.LENGTH_SHORT).show();
}
}
Try this out and implement this logic according to your requirement.
int cntChoice = myList.getCount();
String checked = "";
String unchecked = "";
SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++)
{
if(sparseBooleanArray.get(i) == true)
{
checked += myList.getItemAtPosition(i).toString() + "\n";
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked+= myList.getItemAtPosition(i).toString() + "\n";
}
}
use CHOICE_MODE_MULTIPLE in your ListView and use getCheckedItemPositions() to get the checked ones.
So Onclick of button u can do this,From this you will get the items that are checked:-
#Override
public void onClick(View v)
{
System.out.println("check"+getListView().getCheckItemIds().length);
for (int i = 0; i < getListView().getCheckItemIds().length; i++)
{
System.out.println(getListView().getAdapter().getItem((int)getListView().getCheckItemIds()[i]).toString());
}
}

Categories

Resources