user37375
user37375

Reputation: 169

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


 }

Upvotes: 0

Views: 1061

Answers (1)

MilapTank
MilapTank

Reputation: 10076

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

Upvotes: 1

Related Questions