1

I have a listview in an android app with checkboxes and I'm trying to get a list of the items that are checked. I have tried both setting the checked state in the getView() method with setChecked(true) and by checking it manually by tapping on the item. I've tried two different methods for getting the checked items and both return null. What am I doing wrong?

Greg

    //Called from a menu

    //First attempt - checked is always null
    //I also set setChoiceMode = CHOICE_MODE_MULTIPLE before setting adapter
    //and set no setChoiceMode
    String ItemsChecked = null;
    ListView listview = (ListView) findViewById(R.id.ListLists);

    SparseBooleanArray checked = listview.getCheckedItemPositions();

    for (int i = 0; i < checked.size(); i++) {
        if (checked.get(i)) {
            ItemsChecked += Integer.toString(i) + ",";
        }
    }

   //I then tried this and checked[] is empty
   //I also set setChoiceMode = CHOICE_MODE_NONE before setting adapter
   long checked[] = listview.getCheckedItemIds();

    for (int i = 0; i < checked.length; i++) {
        ItemsChecked += "" + checked[i] + ",";
    }


//Layout for the ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/ic_listit" >
    </ImageView>
        <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />
    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="22sp" >
    </TextView>

</LinearLayout> 
user1091524
  • 865
  • 1
  • 15
  • 28

2 Answers2

1

I am not sure you have provided enough information to solve the problem. But here are some troubleshooting steps that may help point you in the right direction:

1) Instead of getting an array based on the number of checked items in the listView, see if you can pull the array of items in the entire list, or at least the first 100 if the list is very large. Then output the index number of each item in a string like you had in your first attempt. You could also try and output whether the current item is checked or not.

  • this will tell you if you have a populated list to pull from. If you don't, well there's your problem.

2) I would also check to make sure that your listView's name is also correct. I would assume the compiler would catch this but you never know.

3) Finally, I can't see where or how the list is populated. It may be a good idea to check and see the rest of the code to see if maybe something is happening somewhere else.

Sorry if this is not particularly helpful. If all of this checks out and the problem is not being created in one of those areas this link may help:

Android: list.getCheckedItemPositions() always returns null

Good Luck and I hope this helps a little.

Community
  • 1
  • 1
1

Max,

Well, you pointed me in the right direction for a 'solution'. This may not be the proper way to do it, but given this is my my first android app, I am ready to move on after spending the entire morning trying to get a list of just those items that are checked with a built-in method. I modified code from the link you posted and came up with the code below. It seems to work.

I wonder if I will have problems with lists that don't show everything at once. My test only has two items in the list. If items are checked and then scrolled out of view, will this work. More testing needs to be done.

Thanks,

Greg

        ListView listview = (ListView) findViewById(R.id.ListLists);
        View v;
        CheckBox ck;
        TextView tv;
        for (int i = 0; i < listview.getCount(); i++) {
            v = listview.getChildAt(i);
            ck = (CheckBox) v.findViewById(R.id.checkBox1);
            tv = (TextView) v.findViewById(R.id.label);
            if (ck.isChecked()) {
                Toast.makeText(getApplicationContext(), tv.getText() + "Checked" , Toast.LENGTH_LONG)
                .show();
            }
            else {
                Toast.makeText(getApplicationContext(), tv.getText() + "Not checked" , Toast.LENGTH_LONG)
                        .show();
            }
        }
user1091524
  • 865
  • 1
  • 15
  • 28