我有一个两部分的问题。
1)如何填充我的ListView,以便显示字符串,但当选择项目时,使用的实际值是非可见id值(来自手机联系人的联系人ID)?
2)我有一个ListView,它使用多选模式进行项目选择。它由我的联系人列表中的名称填充。当我在ListView中选择一个项目时,我希望该选定项目触发对我的SqLite例程的调用,以将该值存储到数据库记录中。当在listview中选中项目时,如何使此事件触发?
这是我的布局XML;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@+id/lvContacts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
/>
</LinearLayout>
这是我用来填充ListView的代码:
private void fillData() {
final ArrayList<String> contacts = new ArrayList<String>();
// Let's set our local variable to a reference to our listview control
// in the view.
lvContacts = (ListView) findViewById(R.id.lvContacts);
String[] proj_2 = new String[] {Data._ID, Phone.DISPLAY_NAME, CommonDataKinds.Phone.TYPE};
cursor = managedQuery(Phone.CONTENT_URI, proj_2, null, null, null);
while(cursor.moveToNext()) {
// Only add contacts that have mobile number entries
if ( cursor.getInt(2) == Phone.TYPE_MOBILE ) {
String name = cursor.getString(1);
contacts.add(name);
}
}
// Make the array adapter for the listview.
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
contacts);
// Let's sort our resulting data alphabetically.
aa.sort(new Comparator<String>() {
public int compare(String object1, String object2) {
return object1.compareTo(object2);
};
});
// Give the list of contacts over to the list view now.
lvContacts.setAdapter(aa);
}
我本希望能够为每个选中的项目使用类似于 onClick 的事件,但是一直没有任何进展。欢迎提供任何帮助,非常感谢。
ListActivity
内实现方法protected void onListItemClick(ListView l, View v, int position, long id)
相同,因此@nguyendat所说的内容阅读起来更“困难”。 - Caumons