tags:

views:

497

answers:

2

Hey folks,

I need your help. I'm really new to Android, developing in Eclipse with the latest 2.1 SDK.

This is really simple. I make a ListView which is filled with Checkbox widgets.

My code:

public class HelloAndroid extends Activity {

    static final String[] COUNTRIES = new String[] {
        "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
        "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
        "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
        "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium"}; 

    protected ListView myList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myList = (ListView)findViewById(R.id.myList);
        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.row, R.id.myBox, COUNTRIES); 
        myList.setAdapter(myAdapter);
    }
}

And this is the Layout-XML-file (row.xml), consisting of a single checkbox:

<?xml version="1.0" encoding="utf-8"?>
<CheckBox
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myBox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"/>

Now if there are enough strings in the COUNTRIES-Array and you can scroll the List, you will see, that if you check one CheckBox and scroll down, some another CheckBox gets checked automatically. And if you check some more boxes, more get checked automatically, or ones that were checked get unchecked.

I've also tried android:focusable="false" for the CheckBox (in the XML), as well as android:focusableInTouchMode, as described in some similar threads, but nothing seems to help...

Looks like its a bug??

+4  A: 

What's happening is that Android is reusing the CheckBox views it creates to display the list to save memory. Instead of creating a new CheckBox for every item in your list, Android creates just enough to fill the screen, updating them to show the relevant data for the items which are currently visible.

If you have no link between the data in your Adapter and the state of the CheckBox in the view then it will appear that lots of CheckBoxes are linked, when in fact it's the same CheckBox with a different label.

Have you set the choiceMode attribute of your ListView? If you do, this will tell the ListView to keep track of what you have selected.

Also, consider using the CheckedTextView class instead of CheckBox.

Dave Webb
To use the checkbox choiceMode, - use R.layout.simple_list_item_checked as the layout you provide to your ArrayAdapter - set the listView choiceMode to CHOICE_MODE_MULTIPLE
Mayra
Thanks, setChoiseMode(ListView.CHOISE_MODE_MULTIPLE) works like a charm.How can I bind Booleans to the state of the Checkboxes/CheckedTextViews with the SimpleExpandableListAdapter?..
a.tamaz
Not quite sure what you want to do. What do you need that calling `getCheckedItemPositions()` on your `ListView` doesn't provide?
Dave Webb
I was referring to your statement "If you have no link between the data in your Adapter and the state of the CheckBox in the view ..."I thought I could initiate the states of the CheckBoxes/CheckedTextView within my adapter, as I do with their labels.
a.tamaz
You'd have to use a custom Adapter rather than the `SimpleExpandableListAdapter `
Dave Webb
A: 

Dave's right.

Here you'll find a very good video with explanation of ListView behavior: http://www.youtube.com/watch?v=N6YdwzAvwOA

omarcin