getCheckedItemPositions()总是返回null(Android的ListView控件) [英] getCheckedItemPositions() always returning null (Android ListView)

查看:624
本文介绍了getCheckedItemPositions()总是返回null(Android的ListView控件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:基于问题的演变,我编辑了这个问题。

所有的

首先,我知道有一些类似的问题,但他们每个人都有特定的差异,这使得答案没用,我...

我是真的真的AP preciate如果有人能帮助我,我变得非常绝望了这个问题...

所以,问题是:我想填充复选框一个ListView,并且不能使用simple_multiple_choice_mode(类似的东西),因为我需要手动建立我的XML布局 - 所以我使用的list_item.xml文件:

 < XML版本=1.0编码=UTF-8&GT?;
        <复选框
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:以下属性来=8毫米
机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / nomeAPP的风格=?的listItem>
        < /复选框>
 

但问题是,如果我使用simple_multiple(..)模式选项,getCheckedItemPositions工作正常。如果没有(因为我有下面的code)getCheckedItemPositions来空。所以,从我读,这是一个常见的​​错误,这需要一个处理程序解决方法。但我不能让处理器的工作,我在logcat中得到一个例外,显示java.lang.NullPointerException。

谁能帮帮我吗?

我这个小pretty的code:

  this.setListAdapter(新ArrayAdapter<字符串>(这一点,
                    R.layout.list_item,AUX));

            list.setItemsCanFocus(假);
            list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
            list.setSelected(真正的);
            list.setClickable(真正的);
            list.setOnItemSelectedListener(新OnItemSelectedListener(){
                公共无效onItemSelected(适配器视图<>为arg0,查看ARG1,
                        INT ARG2,长ARG3){
                    //自动生成方法存根

                    Log.d(检查,+ ARG2);


                }

                公共无效onNothingSelected(适配器视图<>为arg0){
                    //自动生成方法存根
                }

            });


            复选框C =(复选框)findViewById(R.id.nomeAPP);
            c.setOnCheckedChangeListener(新OnCheckedChangeListener(){
                公共无效onCheckedChanged(CompoundButton为arg0,布尔ARG1){
                    复选框复选框=(复选框)为arg0;
                    布尔器isChecked = checkbox.isChecked();

                    如果(==器isChecked真)
                        Log.v(TAG,真);
                    其他
                        Log.v(TAG,假);
                }
            });
 

解决方案

看起来这是在使用setAdapter的,这是使用setListAdapter解决的问题。

让我们来看看,文档指出ListActivity应该使用

 名单= getListView();
< ListView的机器人:ID =@机器人:ID /列表
 

所以,不要担心=+ @ ID(....

现在,我们可以在我们的活动知道去哪里的名单,现在我们可以使用

  setListAdapter(新ArrayAdapter<字符串>(这一点,
                      R.Layout.List_item,AUX));
 

和一切工作正常。

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

解决方案

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.

这篇关于getCheckedItemPositions()总是返回null(Android的ListView控件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆