samedi 7 avril 2018

ListView with text and checkbox in android

I used RelativeLayout for my ListView.

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

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:text="CheckBox" />

<TextView
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/checkBox1"
    android:layout_alignBottom="@+id/checkBox1"
    android:layout_toRightOf="@+id/checkBox1"
    android:text="TextView" />

</RelativeLayout>

Here is my ListView code,

private void displayList(){
    ArrayList<String> diyListArray = new ArrayList<String>();
    diyListArray.add("1");
    diyListArray.add("2");
    diyListArray.add("3");
    diyListArray.add("4");
    diyListArray.add("5");
    diyListArray.add("6");
    diyListArray.add("7");
    listAdapter = new ArrayAdapter<String>(this, R.layout.selectrow, R.id.test, diyListArray);
    diyList.setAdapter( listAdapter );
}

We can see here that I used ArrayAdapter with resource id of TextView. I am able to display the checkboxes and their corresponding texts.

But with these codes, How can I get user selected texts from checkboxes?




Aucun commentaire:

Enregistrer un commentaire