ListView-无法使OnCheckedChangeListener(复选框)正常工作 [英] ListView - can't get OnCheckedChangeListener (Checkboxes) to work

查看:56
本文介绍了ListView-无法使OnCheckedChangeListener(复选框)正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


所以问题是:我想用复选框填充ListView,并且要自定义ListView,所以我不使用simple_multiple_choice_mode,而是将布局放在list_item.xml中:(对于每一行)

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


So the problem is: I want to populate a ListView with checkboxes, and as I want to customize my ListView I''m not using simple_multiple_choice_mode, I''m putting my layout on list_item.xml: (for each row)

        <checkbox>
xmlns:android="http://schemas.android.com/apk/res/android" android:paddingLeft="8mm"
android:layout_width="fill_parent"
        android:layout_height="wrap_removed" android:id="@+id/nomeAPP" style="?listItem">
        </checkbox>



我的ListView位于lista.xml



My ListView is on lista.xml

<ListView android:id="@android:id/list"

        android:layout_width="fill_parent" android:layout_height="wrap_content"

        style="?whiteBackground">

</ListView>



我的适配器:



my adapter:

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




因此,我试图为复选框创建一个setOnCheckedChangeListener,以便将所选的项目存储在数组中.

所以我根据以下答案做了这个监听器:




So I''m trying to make a setOnCheckedChangeListener for the checkboxes, so I can store the chosed items in an array.

So I did this listener based on the answer below:

CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01);
            cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(CompoundButton buttonView,
                        boolean isChecked) {
                    // TODO Auto-generated method stub
                    if (buttonView.isChecked()) {
                        Toast.makeText(getBaseContext(), "Checked",
                                Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        Toast.makeText(getBaseContext(), "UnChecked",
                                Toast.LENGTH_SHORT).show();
                    }

                }
            });



问题是:我得到了NULL异常. CURIOUS 的事情是:如果我切换"布局以使用简单的



Problem is: I get the NULL exception. The CURIOUS thing is: if I "switch" my layout for a simple

<ScrollView android:id="@+id/widget54" android:layout_width="fill_parent"

android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout android:orientation="vertical"

android:layout_width="fill_parent" android:layout_height="fill_parent">
<CheckBox android:text="Checkbox" android:id="@+id/CheckBox01"

android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</LinearLayout>

</ScrollView>



忘了我的ListView,它起作用了!那么,我的布局有什么问题?

我创建了一个新的小项目,目的只是为了让大家看到确切的情况.在此处 [



And forget my ListView, it works! So, what''s wrong with my layout?

I created a new little project just to let you guys see exactly the situation. It''s available here[^] Right now does not work, but if you uncomment the adapter, change ListActivity to Activity and change setContentView, it will work (checked/unchecked toast text).

Another curious thing I found is that with ListActivity does not work never.... wondering if it''s related...

推荐答案

我上次回答:http ://www.codeproject.com/Questions/253369/Problem-getting-Checkbox-items-on-the-ListView

这仍然是一个普遍的问题.

阅读该stackoverflow-thread的答案,您会发现:

I answered last time: http://www.codeproject.com/Questions/253369/Problem-getting-Checkbox-items-on-the-ListView

and it is still a common problem.

Read the answers of that stackoverflow-thread and you''ll find this:

private void addClickHandlerToCheckBox(CheckBox checkbox) {
    checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            CheckBox checkbox = (CheckBox)arg0; 
            boolean isChecked = checkbox.isChecked();
            // Store the boolean value somewhere durable
        }
    }
);



那么您可以访问持久存储"中存储的值.



您需要将侦听器添加到复选框-否则您将不知道哪个复选框已被选中.



then you can access the value that you have stored "somewhere durable".



You need to add the listener to the checkbox - otherwise you will not know which checkbox has been hit.

   // HashMap as member variable to store state of the checkboxes
   // needs to be final to be accessed in anonymous listener class
   // Don't be scared by the Key and Value, those work as fine as int and boolean
   private final HashMap<integer,> oMapCheckBoxSelection = new HashMap<integer,>;

   // ID's for the Checkboxes
   // store as variable to have a common reference
   private final static int
     iCheckBoxXY = 0,
     iCheckBoxXZ = 1;
{
   
   // ...

   CheckBox oCheckBoxXY = (CheckBox) findViewById(R.id.nomeAPP);
   oCheckBoxXY.setID();
   addClickHandlerToCheckBox(oCheckboxXY);


   // ....

   // Check if the checkbox is in the Hashmap and read it.
   if(oMapCheckBoxSelection.contains(iCheckBoxXY)){
     boolean isCheckboxSelected = oMapCheckBoxSelection.get(iCheckBoxXY);
   }


}

private void addClickHandlerToCheckBox(CheckBox checkbox) {
    checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
            CheckBox checkbox = (CheckBox)arg0; 
            oMapCheckBoxSelection.put(checkbox.getID(), checkbox.isChecked());
        }
    }
);



问题是获取正确的初始化HashMap值,并在尝试读取该值之前验证它是否已初始化.
您还可以在初始化复选框时初始化HashMap值:
oMapCheckBoxSelection.put(oCheckBoxXY.getID(), oCheckBoxXY.isChecked());
...但是我想更喜欢使用HashMap.contains进行检查,以确保它是.



Problem is to get a proper initialized HashMap Value and to verify if it is initialized before you try to read that value.
You might also init the HashMap-Value when you init the Checkbox:
oMapCheckBoxSelection.put(oCheckBoxXY.getID(), oCheckBoxXY.isChecked());
...but I''d like to prefer the check with HashMap.contains to make sure it is.


这篇关于ListView-无法使OnCheckedChangeListener(复选框)正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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