检查Android ListView中的多个项目

时间:2014-11-19 01:41:21

标签: android listview

我试图在我的对话框中的列表视图中检查多个项目。

这是Java代码:

            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();

它显示我的对话框中的列表没问题,但是当我点击/按下某个项目时没有任何反应。我没有看到任何标记或任何可视的内容,表明它已被选中/检查。

这是我的XML代码:

<?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>

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您需要更改:

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

致:

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