Solved

Checkbox and ListView in Android Layout

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java SE 8u111  Lot of stuff broke 11 54
Excel file not created as expected 7 51
MS Access XML API HTTP POST Call Object Send Not Working Correctly 15 38
difference of if loops 23 43
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

863 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

27 Experts available now in Live!

Get 1:1 Help Now