Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
EDIT: 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>


My ListView is on lista.xml

XML
<ListView android:id="@android:id/list"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        style="?whiteBackground">

</ListView>


my adapter:

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



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:

C#
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();
                    }

                }
            });


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

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


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...
Posted
Updated 14-Sep-11 3:51am
v6

1 solution

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:

Java
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".

EDIT:

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

Java
   // 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());
        }
    }
);


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.
 
Share this answer
 
v2
Comments
Maxdd 7 13-Sep-11 11:58am    
I'm sorry, I really thought that I previously asked at another forum. But I don't understand that solution, why should I store a boolean value? Because what I want is to store all options in an array.
TorstenH. 13-Sep-11 12:44pm    
..then make it an array. I expect you to be a developer - you probably have heard of the type HashMap and are able to use it:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/HashMap.html

The solution is needed as the function getCheckedItemPositions() is ALWAYS returning NULL - that's a old bug and should be known.
Maxdd 7 13-Sep-11 18:28pm    
@TorstenHn, never mind I was not thinking straight. can you please read my edited question?
TorstenH. 14-Sep-11 1:27am    
check my edit too.
You added the listener on the wrong item - it should be directly attached to the checkbox.
Maxdd 7 14-Sep-11 9:15am    
I tried that but has some errors so for someone who obviously don't know what's doing is not easy. However, I don't believe that is necessary because the previous solution (with that code) already worked, but to another layout. Please see me edited question again!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900