Link to home
Start Free TrialLog in
Avatar of Cesar Aracena
Cesar AracenaFlag for Argentina

asked on

Checkbox and ListView in Android Layout

Hola!

I'm trying to learn some Android programming and I've encountered something very odd. I have the following code:

    private void populateListView() {
        Cursor cursor = myDb.getAllRows();
        final String[] fromFieldNames = new String[] {DBAdapter.KEY_PLAYER, DBAdapter.KEY_ROWID};
        final int[] toViewIDs = new int[] {R.id.textViewPlayerName, R.id.textViewPlayerId};
        SimpleCursorAdapter myCursorAdapter;
        myCursorAdapter = new SimpleCursorAdapter(getBaseContext(), R.layout.item_layout, cursor, fromFieldNames, toViewIDs, 0);
        ListView myList = (ListView) findViewById(R.id.players_list);
        myList.setAdapter(myCursorAdapter);

        myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
                //Add code for each item here. Use either position or id to differentiate items
                ListView lv = (ListView) parent;
                if (lv.isItemChecked(position)) {
                    Toast.makeText(getBaseContext(), "You checked " + position, Toast.LENGTH_SHORT).show();
                    lv.setItemChecked(position, true);
                }else{
                    Toast.makeText(getBaseContext(), "You unchecked " + position, Toast.LENGTH_SHORT).show();
                    lv.setItemChecked(position, false);
                }
            }
        });
    }

Open in new window

It populates a listView from a database but when I click on any item, the checkbox is not checked. It always throws me the last "Unchecked" toast. It just won't stay checked.

I've already tried the "make the checkbox not focusable" trick I found everywhere but that doesn't help. Any ideas?

Just in case, here's the XML for part of the ListView:

    <CheckBox
        android:id="@+id/checkBoxPlayerSelected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:text="" />

    <TextView
        android:id="@+id/textViewPlayerName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/checkBoxPlayerSelected"
        android:layout_alignBottom="@+id/checkBoxPlayerSelected"
        android:layout_toRightOf="@+id/checkBoxPlayerSelected"
        android:text="TextView" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textViewPlayerName"
        android:layout_toRightOf="@+id/textViewPlayerName"
        android:layout_toEndOf="@+id/textViewPlayerName"
        android:id="@+id/textViewPlayerId"
        android:visibility="invisible" />

Open in new window

Any clues?

Thanks!
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

You are setting your itemClickListener to the adapterView. Try setting it to the listView

//myList.setOnItemClickListener(new AdapterView.OnItemClickListener() 

//Try this instead
 myList.setOnItemClickListener (new OnItemClickListener() 

Open in new window

Avatar of Cesar Aracena

ASKER

It tells me "Cannot resolve symbol 'OnItemClickListener'" and upon hitting Alt+Enter it resolves again to AdapterView.OnItemClickListener()
ASKER CERTIFIED SOLUTION
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial