开发者

getCheckedItemPositions() always returning null (Android ListView)

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 need开发者_如何学JAVAs 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");
                }
            });


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


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜