Solved

Checkbox and ListView in Android Layout

Posted on 2016-11-01
4
65 Views
Last Modified: 2016-11-20
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!
0
Comment
Question by:Caracena
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Chris Harte
ID: 41870199
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

0
 
LVL 6

Author Comment

by:Caracena
ID: 41871033
It tells me "Cannot resolve symbol 'OnItemClickListener'" and upon hitting Alt+Enter it resolves again to AdapterView.OnItemClickListener()
0
 
LVL 17

Accepted Solution

by:
Chris Harte earned 500 total points
ID: 41872034
"Cannot resolve symbol 'OnItemClickListener'"

That is because you have not implemented it in your activity. eg
public class This_Activity extends Activity implements View.OnClickListener

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 41
Adding multiple JVM environments to RedHat 6 7 22
sql import cannot be resolved jsp 3 23
QQ problem 22 23
If your app took Google’s lash recently, here are the 5 most likely reasons.
Here, we have shared an easy step by step tutorial on how to download instagram images and videos on PC, Android Mobile and iOS mobile.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question