Solved

Checkbox and ListView in Android Layout

Posted on 2016-11-01
4
118 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:Cesar Aracena
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Cesar Aracena
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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The case of the missing phone talks about the way a small electronic gadget (the mobile phone) has penetrated into our lives and has made us addicted to it.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

752 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