Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

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();
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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() + "
";
     }
     else  if(sparseBooleanArray.get(i) == false) 
     {
         unchecked+= myList.getItemAtPosition(i).toString() + "
";
     }

 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...