b85411
b85411

Reputation: 10010

Checking multiple items in a Android ListView

I am trying to get multiple items checked in a list view I have in my dialog box.

This is the Java code:

            String names[] ={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"};
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
            LayoutInflater inflater = activity.getLayoutInflater();
            View convertView = (View) inflater.inflate(R.layout.test, null);
            alertDialog.setView(convertView);
            alertDialog.setTitle("List");
            ListView lv = (ListView) convertView.findViewById(R.id.my_list);
            lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);   
            lv.setItemsCanFocus(false);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, names);
            lv.setAdapter(adapter);
            alertDialog.show();

It shows the list in my dialog box okay, but when I click/press an item nothing happens. I don't see any tick or anything visually to indicate it's been selected/checked.

Here's my XML code:

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

    <ListView 
        android:id="@+id/my_list"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />

  </LinearLayout>

What am I doing wrong?

Upvotes: 0

Views: 85

Answers (1)

squalle0nhart
squalle0nhart

Reputation: 351

You need to change :

ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, 
android.R.layout.simple_list_item_1, names);

To :

ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,     
android.R.layout.simple_list_item_mutiple_choice, names);

Upvotes: 2

Related Questions