我正在尝试将联系人显示在列表视图中。现在,我知道使用simple_list_item_multiple_choice可以选择多个联系人,但是只能查看姓名而没有数字。

另一方面,simple_list_item_2可用于显示姓名和电话号码,但仅支持选择一个联系人。

是否有将两者结合的模板?如果没有,如何使用这两个功能构建自定义列表?

编辑:这是我正在使用的代码

CursorLoader cl = new CursorLoader(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
Cursor c = cl.loadInBackground();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, // Use a template
                                                         // that displays a
                                                         // text view
                    c, // Give the cursor to the list adapter
                    new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
                    new int[] { android.R.id.text1},0);

setListAdapter(adapter);


在这里,SimpleCursorAdapter的第二个参数是simple_list_item_multiple_choice,但它仅支持处理android.R.id.text1。因此,我只能使用项目,不能使用子项目。

但是在下面的代码中

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_2, // Use a template
                                                         // that displays a
                                                         // text view
                    c, // Give the cursor to the list adapter
                    new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ,ContactsContract.CommonDataKinds.Phone.NUMBER},
                    new int[] { android.R.id.text1,android.R.id.text2},0);


我可以同时用ContactsContract.CommonDataKinds.Phone.DISPLAY_NAMENUMBER编写它,但不能使用多选功能。

最佳答案

正如Dipu所说,您应该制作自己的自定义布局。
要显示姓名和联系方式,您需要两个文本视图和一个复选框进行检查。

您可以从本教程开始编码:

http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

在country_info.xml中再添加一个文本视图将解决您的问题。

添加

要使用自定义列表视图布局,您必须实现自己的适配器。

本教程(2. Custom ArrayAdapter示例)将帮助您弄清楚该如何做。

http://www.mkyong.com/android/android-listview-example/

10-07 19:48
查看更多