get checked items from listview in android - 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());
}
}

Related

Output of listview getItemAtPosition() method gives unreadable output

My listview is from a layout where its id is #android:id/list. It is part of the android.R.id.list. I used a SimpleCursorAdapter to setAdapter to listview. The listview contains checkboxes with contact names such as Emily Andrews, Susan John... I wanted to know the names of the contacts which were selected by the user. When I use the code below, it prints an object android.content.ContentResolver.cursorWrapper... I am not sure how to interpret this and know which contact name was selected. Is there a way I can make the output of listview.getItemAtPosition() readable?
Code:
public void blockCheckedItems(View view) {
// int cntChoice = lv.getCount();
checked = new ArrayList<String>();
unchecked = new ArrayList<String>();
int itempositions=adapter.getCount();
SparseBooleanArray sparseBooleanArray = lv.getCheckedItemPositions();
int countChoice = lv.getChildCount();
Log.d("" + countChoice, "CountChoice===============================");
Log.d("" + sparseBooleanArray,"sparseBooleanArray -------------------------");
for(int i = 0; i < countChoice; i++)
{
if(sparseBooleanArray.get(i) == true)
{
checked.add(lv.getItemAtPosition(i).
.toString());
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked.add(lv.getItemAtPosition(i).toString());
}
}
for(int i = 0; i < checked.size(); i++){
Log.d("checked list&&&&&&&&&&&&&&&&&&&", "" + checked.get(i));
}
for(int i = 0; i < unchecked.size(); i++){
Log.d("in unchecked list&&&&&&&&&&&&&&&&&&&", "" + unchecked.get(i));
}
}
When listview adapter is a cursor adapter then listview.getItemAtPosition() return a cursor pointed to that row. So you have to get data from that cursor like this:
Cursor cursor = listview.getItemAtPosition();
String name = cursor.getString(0);
I just assume that 0 is the index of first name column in that database which cursor is representing, It could be 2 , 3 ...

ListView getItemAtPosition(i) Method gives unreadable object

I wanted to view the items selected in my listview with checkboxes. However, when I try to print out lv.getItemAtPosition(i).toString() it gives me a `` object. How can I convert this to a readable format?
public void blockCheckedItems(View view) {
// int cntChoice = lv.getCount();
checked = new ArrayList<String>();
unchecked = new ArrayList<String>();
int itempositions=adapter.getCount();
SparseBooleanArray sparseBooleanArray = lv.getCheckedItemPositions();
int countChoice = lv.getChildCount();
Log.d("" + countChoice, "CountChoice===============================");
Log.d("" + sparseBooleanArray,"sparseBooleanArray -------------------------");
for(int i = 0; i < countChoice; i++)
{
if(sparseBooleanArray.get(i) == true)
{
checked.add(lv.getItemAtPosition(i).toString());
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked.add(lv.getItemAtPosition(i).toString());
}
}
for(int i = 0; i < checked.size(); i++){
Log.d("checked list&&&&&&&&&&&&&&&&&&&", "" + checked.get(i));
}
for(int i = 0; i < unchecked.size(); i++){
Log.d("in unchecked list&&&&&&&&&&&&&&&&&&&", "" + unchecked.get(i));
}
}
try something like this:
if(sparseBooleanArray.get(i) == true)
{
checked.add(Boolean.toString(sparseBooleanArray.get(i)));
}
else if(sparseBooleanArray.get(i) == false)
{
unchecked.add(Boolean.toString(sparseBooleanArray.get(i)));
}
I have solved the above questions by changing the following code and using Cursor and keeping everyother part of the above code the same.
for(int i = 0; i < countChoice; i++)
{
if(sparseBooleanArray.valueAt(i) == true)
{
Cursor cursor = (Cursor) lv.getItemAtPosition(i);
String name = cursor.getString(1);
checked.add(name);
}
else if(sparseBooleanArray.valueAt(i) == false)
{
Cursor cursor = (Cursor) lv.getItemAtPosition(i);
String name = cursor.getString(1);
unchecked.add(name);
}
}

SparseBooleanArray giving true for unchecked items in listview

My listview is from a layout where its id is #android:id/list. It is part of the android.R.id.list. I used a SimpleCursorAdapter to setAdapter to listview. The listview contains checkboxes with contact names such as Emily Andrews, Susan John... I wanted to know the names of the contacts which were selected by the user. When using the code below, for some reason, it gives me random contact names in my unchecked list. For instance though I have Emily checked, and Matt Jones unchecked. It has both of them in the unchecked list. Also, no matter what i check and uncheck in the listview, it always gives me the first two contacts in my unchecked list and doesn't print anything for my checked list. Is there any way I can solve this?
public void blockCheckedItems(View view) {
// int cntChoice = lv.getCount();
checked = new ArrayList<String>();
unchecked = new ArrayList<String>();
int itempositions=adapter.getCount();
SparseBooleanArray sparseBooleanArray = lv.getCheckedItemPositions();
int countChoice = lv.getChildCount();
Log.d("" + countChoice, "CountChoice===============================");
Log.d("" + sparseBooleanArray,"sparseBooleanArray -------------------------");
for(int i = 0; i < countChoice; i++)
{
if(sparseBooleanArray.valueAt(i) == true)
{
Cursor cursor = (Cursor) lv.getItemAtPosition(i);
String name = cursor.getString(1);
checked.add(name);
}
else if(sparseBooleanArray.valueAt(i) == false)
{
Cursor cursor = (Cursor) lv.getItemAtPosition(i);
String name = cursor.getString(1);
unchecked.add(name);
}
}
for(int i = 0; i < checked.size(); i++){
Log.d("checked list&&&&&&&&&&&&&&&&&&&", "" + checked.get(i));
}
for(int i = 0; i < unchecked.size(); i++){
Log.d("in unchecked list&&&&&&&&&&&&&&&&&&&", "" + unchecked.get(i));
}
}
try this and let me know if i am wrong
public void blockCheckedItems(View view) {
ArrayList<String> checked = new ArrayList<String>();
ArrayList<String> unchecked = new ArrayList<String>();
SparseBooleanArray sparseBooleanArray = listView.getCheckedItemPositions();
for (int i = 0; i < listView.getCount(); i++) {
Cursor cursor = (Cursor) listView.getItemAtPosition(i);
String name = cursor.getString(1);
if (sparseBooleanArray.get(i)) {
checked.add(name);
} else {
unchecked.add(name);
}
}
for (int i = 0; i < checked.size(); i++) {
Log.d("checked list&&&&&&&&&&&&&&&&&&&", "" + checked.get(i));
}
for (int i = 0; i < unchecked.size(); i++) {
Log.d("in unchecked list&&&&&&&&&&&&&&&&&&&", "" + unchecked.get(i));
}
}

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.

Get number of selected items in android ListView

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++;
}
}

Categories

Resources