jeudi 24 décembre 2015

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://ift.tt/nIICcg"
    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://ift.tt/nIICcg"
    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?




Aucun commentaire:

Enregistrer un commentaire