Solved

Checkbox and ListView in Android Layout

Posted on 2016-11-01
4
36 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 16

Expert Comment

by:Chris Harte
Comment Utility
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
Comment Utility
It tells me "Cannot resolve symbol 'OnItemClickListener'" and upon hitting Alt+Enter it resolves again to AdapterView.OnItemClickListener()
0
 
LVL 16

Accepted Solution

by:
Chris Harte earned 500 total points
Comment Utility
"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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

763 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now