Paweł Poręba
Paweł Poręba

Reputation: 1105

android, listview with checked box

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?

Upvotes: 1

Views: 90

Answers (2)

tasomaniac
tasomaniac

Reputation: 10342

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.

Upvotes: 2

Remees M Syde
Remees M Syde

Reputation: 2609

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

            }
        });

Upvotes: -1

Related Questions