Cesar Aracena
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:
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:
Thanks!
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);
}
}
});
}
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" />
Any clues?Thanks!
ASKER
It tells me "Cannot resolve symbol 'OnItemClickListener'" and upon hitting Alt+Enter it resolves again to AdapterView.OnItemClickLis tener()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window