9

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();
        }
    }
maxivis
  • 1,727
  • 3
  • 21
  • 35
sanjay
  • 2,590
  • 17
  • 55
  • 88

3 Answers3

29

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

 }
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
stella
  • 441
  • 4
  • 12
4

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

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
Sujit
  • 10,512
  • 9
  • 40
  • 45
  • its working fine.Its getting selected names.But how could i get Unchecked names too? – sanjay Jan 04 '12 at 11:59
  • if you want the names of both checked and unchecked items just use `getCount()` and `getItemAtPosition(int position)`. you could iterate through the items and add them to your ArrayList. see [getCount](http://developer.android.com/reference/android/widget/AdapterView.html#getCount()) and [getItemAtPosition](http://developer.android.com/reference/android/widget/AdapterView.html#getItemAtPosition(int)) – kmera Jan 04 '12 at 12:08
0

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());                 
        }   

}
stella
  • 441
  • 4
  • 12
  • 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(); – sanjay Jan 04 '12 at 11:48
  • From that i can get checked items well.But how could i get Unchecked itemm too separately – sanjay Jan 04 '12 at 12:12