如何在自定义listView上设置CHOICE_MODE_MULTIPLE

时间:2012-02-22 08:38:25

标签: android android-listview

我有一个像这样的自定义ListView:

List<Journal_Database> values = localDatabase.getAllJournalNames();
ListView journalNames = (ListView)findViewById(R.id.JournalList);
journalNames.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
journalNames.setAdapter(new ArrayAdapter<Journal_Database>(
            this,R.layout.journal_name_list_black_text,R.id.list_content, values));

我如何设置CHOICE_MODE_MULTIPLE?

我试过了:

 journalNames.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

但没有任何反应。

编辑:

我找到了解决方案,您可以将SDK文件夹中的choice_mode_multiple.xml文件复制到项目中并进行更改。

我现在有这个:

<?xml version="1.0" encoding="utf-8"?>

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:textColor="#000000"
android:textSize="15dp"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>

在java代码中:

List<Journal_Database> values = localDatabase.getAllJournalNames();
    ListView journalNames = (ListView)findViewById(R.id.JournalList);
    journalNames.setAdapter(new ArrayAdapter<Journal_Database>(
            this,R.layout.simple_list_item_multiple_choice,android.R.id.text1, values));
    journalNames.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

谢谢

1 个答案:

答案 0 :(得分:3)

请看这个链接。它可能有用... http://www.vogella.de/articles/AndroidListView/article.html

这样的一些例子......

public class MyList extends ListActivity {


/** Called when the activity is first created. */

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // Create an array of Strings, that will be put to our ListActivity
    String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse",
            "Ubuntu", "Solaris", "Android", "iPhone", "Linux", "Windows7",
            "Eclipse", "Suse", "Ubuntu", "Solaris", "Android", "iPhone" };
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice,
            android.R.id.text1, names));
    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

}