Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get checked items from listview in android

Tags:

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();
        }
    }
like image 526
sanjay Avatar asked Jan 04 '12 11:01

sanjay


2 Answers

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

 }
like image 129
stella Avatar answered Nov 15 '22 21:11

stella


use CHOICE_MODE_MULTIPLE in your ListView and use getCheckedItemPositions() to get the checked ones.

like image 39
Sujit Avatar answered Nov 15 '22 20:11

Sujit