user37375
user37375

Reputation: 173

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


 }

Upvotes: 1

Views: 1836

Answers (1)

Sadegh
Sadegh

Reputation: 2669

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 ...

Upvotes: 2

Related Questions