CheckedTextView not checked
Asked Answered
A

1

5

I want a Multiselected (checked) listview. When I select an item then the green check mark must appear. For this I use CheckedTextViews. The ListView gets the data from the database. I'm using a SimpleCursorAdapter for that. When you click on the button then the selected entries(IDs) will be passed to the next activity.

My problem is that the check marks of the CheckedTextView does not appear. But the IDs will be passed to the next activity. What am I doing wrong? How to fix it?

selecttest.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

        <Spinner
        android:id="@+id/spinner_select_language"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

        <Button
            style="@style/btn_Font"
            android:id="@+id/selecttest_start"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/spinner_test"
            android:onClick="onClick"
            android:text="@string/selecttest_start" />

        <ListView
            android:id="@+id/lv_lesson"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:fadeScrollbars="false"
            android:choiceMode="multipleChoice"
            android:layout_alignParentLeft="true"
            android:cacheColorHint="#00000000"
            android:layout_below="@+id/selecttest_start" >
        </ListView>

</RelativeLayout>

dataset_ctv_lesson.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <CheckedTextView
        style="@style/tv_Font"
        android:id="@+id/ctv_lesson"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/none" 
        android:checkMark="@drawable/ctv_state_checker"
        />

</RelativeLayout>

ctv_state_checker.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="false"
          android:drawable="@drawable/btn_check_buttonless_off" />
    <item android:state_checked="true"
          android:drawable="@drawable/btn_check_buttonless_on" />

</selector>

SelectTestActivity.java

public class SelectTestActivity 
extends Activity
implements OnItemSelectedListener 
{
    Database db;
    SimpleCursorAdapter adaptercursor, lv_adaptercursor;
    ListView lv_lesson;

    // Arraylist for checked item in the lesson view
    ArrayList<String> checkedlessons = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selecttest);

        // Create Database
        db = new Database(this);

        // Drop-Down-Menu to select a language
        Spinner spinner_language = (Spinner) findViewById(R.id.spinner_select_language);
        spinner_language.setOnItemSelectedListener(this);

        Cursor cursor = db.createListViewCursor();

        String[] displaycolumn = new String[]{"language"};
        int[] displayview = new int[] {R.id.tv_language};

        adaptercursor = new SimpleCursorAdapter(this, R.layout.datasetlanguage, cursor, displaycolumn, displayview, 0);
        spinner_language.setAdapter(adaptercursor);

        // ListView to select a lesson
        lv_lesson = (ListView) findViewById(R.id.lv_lesson);

        cursor = db.createListViewLessonCursor(getSelectedItemIDFromSpinnerLanguage());

        displaycolumn = new String[]{"lesson"};
        int[] displayview2 = new int[] {R.id.ctv_lesson};

        lv_adaptercursor = new SimpleCursorAdapter(this, R.layout.dataset_ctv_lesson, cursor, displaycolumn, displayview2, 0);
        lv_lesson.setAdapter(lv_adaptercursor);

        lv_lesson.setOnItemClickListener(new ListView.OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {
                if (!checkedlessons.contains(Long.toString(id)))
                {
                    checkedlessons.add(Long.toString(id));

                    // checked textview
                    lv_lesson.setItemChecked(position, true);
                }
                else 
                {
                    checkedlessons.remove(Long.toString(id));

                    // unchecked textview
                    lv_lesson.setItemChecked(position, false);      
                }
            }
        });

        // Close the database
        db.close();
    }
Appeal answered 11/12, 2012 at 9:25 Comment(0)
S
3
  1. Try to remove the style of your CheckedTextView, i think some values in your style affect the appearance.
  2. Remove the RelativeLayout in dataset_ctv_lesson.xml, and you do not need to change the check state on item clicked. ListView could maintain the check state by itself. Use ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) to enable multi-select mode, and use ListView.getCheckedItemPositions() to get the checked row positions.
Swivet answered 11/12, 2012 at 10:22 Comment(7)
@user1891764 Do you understand the reason?Swivet
The SimpleCursorAdapter requires a layout file and views which should display the columns of the cursor. The layout file should contain only these views. Is that right?Appeal
No, the layout could have any other views. you can read the source code of ListView. setItemChecked() will try to check a whole row, which means the row view should be checkable. A RelativeLayout is apparently not a checkable view. Besides, you should try to understand the second tip, it's the recommended way to do such things.Swivet
Ok, but if I change the CheckedTextView to a TextView which is not a checkable view it also works.Appeal
How do you define 'it works'. A TextView does not have a check mark. setItemChecked() won't perform any changes to it.Swivet
I think that is a misunderstanding. I don't use setItemChecked() anymore. I removed the RelativeLayout and setOnItemClickListener. With 'it works' I mean that when I use a TextView the check mark does not appear but the IDs will be passed to the next Activity. CheckedTexts works fine, check marks appears and IDs also passed to the next acitivity. To get the IDs I use getCheckedItemIds() from the ListView and getCheckedItemIds().length to get currently selected number of items(IDs), because getCheckedItemCount() was added in API 11 and I use minSdkVersion 8.Appeal
Yeah, the appearance is all correct. getCheckedItemIds can still work with TextView, the the listview will try to check the row view, but will affect the following code.Swivet

© 2022 - 2024 — McMap. All rights reserved.