0

EDIT: Based on evolution of the problem, I edited this question.

First of all, I know there some similar questions, but each one of them has a specific difference, that makes the answer useless for me...

I really really appreciate if anyone can help me, I'm getting really desperate with this problem...

So the problem is: I want to populate a ListView with checkboxes, and can't use simple_multiple_choice_mode (something like that) because I need to manually build my XML layout - so I'm using the list_item.xml file:

<?xml version="1.0" encoding="utf-8"?>
        <CheckBox 
xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="8mm"
android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:id="@+id/nomeAPP" style="?listItem">
        </CheckBox>

Problem is, if I use the simple_multiple (..) mode option, getCheckedItemPositions works fine. If don't (as I have in the code below) getCheckedItemPositions comes null. So from what I read, it's a common bug, that needs an handler as workaround. But I cant get the handler to work, I get an exception with java.lang.NullPointerException in the logcat.

Can anyone please help me?

I have this little pretty code:

this.setListAdapter(new ArrayAdapter<String>(this, 
                    R.layout.list_item, aux));

            list.setItemsCanFocus(false);
            list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            list.setSelected(true);
            list.setClickable(true);
            list.setOnItemSelectedListener(new OnItemSelectedListener() {
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    //  Auto-generated method stub

                    Log.d("checked",""+arg2);        


                }

                public void onNothingSelected(AdapterView<?> arg0) {
                    // Auto-generated method stub
                }

            });


            CheckBox c = (CheckBox) findViewById(R.id.nomeAPP);
            c.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                    CheckBox checkbox = (CheckBox)arg0; 
                    boolean isChecked = checkbox.isChecked();

                    if(isChecked == true)
                        Log.v("TAG", "true");
                    else
                        Log.v("TAG", "false");
                }
            });
Tiago
  • 1,116
  • 5
  • 25
  • 39

4 Answers4

3

try this code

list = (ListView) findViewById(R.id.listagem);
list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item,R.id.nomeAPP,aux));
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

//get the checked Items like this;

int count=list.getChildCount();
for(int i=1;i<=count;i++){
     if(list.isItemChecked(i)){
         //do your task/work
    }
}
Balaji.K
  • 8,745
  • 5
  • 30
  • 39
0

You have to set the choiceMode attribute on your ListView in order to get an object back from this method:

http://developer.android.com/reference/android/widget/AbsListView.html#getCheckedItemPositions%28%29

Rich
  • 36,270
  • 31
  • 115
  • 154
  • I used the suggestion from another answer, and does not work. – Tiago Sep 13 '11 at 18:50
  • Because it seems like you're using your own check box. getCheckedItemPositions is going to use the checkbox that the ListView provides once you set choiceMode to multi. If you want to provide your own CheckBox in your list item layout, you need to use an OnCheckedChangedListener with it and do something with the information this provides to keep track of what is checked. – Rich Sep 13 '11 at 22:08
  • that's what I'm doing. Can you please check my updated answer. – Tiago Sep 13 '11 at 22:20
0

Looks that was a bug in the use of setAdapter, which is solved using setListAdapter.

Let's see, documentation states that ListActivity should use

list=getListView();
<ListView android:id="@android:id/list"

so forget about = "+@id(....

now we can our activity knows "where" is the List and now we can use

setListAdapter(new ArrayAdapter<String>(this, 
                      R.Layout.List_item, aux));

and everything works fine.

Tiago
  • 1,116
  • 5
  • 25
  • 39
0

Your problem is that the custom row should implement Checkable. Read this question.

Community
  • 1
  • 1
Macarse
  • 91,829
  • 44
  • 175
  • 230