1

I've read some posts about this matter but still I can't get it to working. I want to have a listview with a checkbox. Here's my activity_choose_songs.xml:

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

    <TextView
        android:id="@+id/songs_to_choose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/sd_cards_playlist"
        android:textStyle="bold"
        android:gravity="center"
        android:textSize="30sp"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        />

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="@drawable/list_selector"
        android:divider="#242424"
        android:dividerHeight="1dp"
        android:layout_below="@+id/songs_to_choose">
    </ListView>

</RelativeLayout>

And here's the layout for a specific item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="5dp"
    android:background="@drawable/list_selector">

    <CheckedTextView
        android:id="@+id/checkedTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:padding="10dp"
        android:gravity="center"
        android:drawableStart="?android:attr/listChoiceIndicatorMultiple"
        android:textColor="#f3f3f3" />

</LinearLayout>

Here's extract from onCreate() method in an activity where I want this listview to be displayed:

ListAdapter adapter = new SimpleAdapter(getApplicationContext(),chosenSongsListData,
                R.layout.playlist_checked_item,new String[]{"songTitle"},new int[]{R.id.checkedTextView});

        setListAdapter(adapter);
        ListView lw = getListView();

        lw.setItemsCanFocus(false);
        lw.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);

        lw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

Yet, when I'm starting the activity and I' trying to check it, it looks like (the screen is in pressed state): enter image description here

What's wrong with my approach?

Paweł Poręba
  • 1,084
  • 1
  • 14
  • 37

2 Answers2

2

I think the problem is you have a LinearLayout inside your item view. It cannot propagates its status to the children.

I think you have 2 options.

First one is to remove LinearLayout. Your view is not implementing Checkable right now. When you remove it CheckedTextView should work.

The second option is to use CheckedLinearLayout and have your children have this attribute:

  android:duplicateParentState="true"`

You can find the implementation for CheckedLinearLayout online.

tasomaniac
  • 10,234
  • 6
  • 52
  • 84
-1

You have to do it in your on item click listener, The sample code given below.

       lw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            CheckBox check = (CheckBox) view.findViewById(R.id.checkedTextView);

            check.toggle(); //check or un-check the checkbox

            }
        });
Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
  • Never change the state of the view manually. Only `getView` method should change it. This will not survive scroll and config change where the views are recreated. – tasomaniac Dec 24 '15 at 14:13
  • @tasomaniac , Yeah, Thanks for your comment. I just suggest him because he mentioned "unable to check the check box by clicking the list item" ,And i thought this is the simplest way to do that. I didn't think about retain the state :) . – Remees M Syde Jan 04 '16 at 14:18