我将联系人加载到带有复选框的列表视图中,现在我想返回仅选中的联系人的详细信息,例如姓名和电话号码。我将如何获取详细信息?存储在列表视图中的详细信息(例如位置、id 等)在哪里。给出以下代码
String[] from = { "Name", "Phone","chkbox" };
int[] to = { R.id.txtContactName, R.id.txtContactNumber,R.id.checkBox1 };
ArrayList<Map<String,String>> list=buildData();
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.main, from, to);
setListAdapter(adapter);
private ArrayList<Map<String,String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.clear();
Cursor people = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
while (people.moveToNext()) {
String contactName = people.getString(people
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contactId = people.getString(people
.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = people
.getString(people
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ((Integer.parseInt(hasPhone) > 0)) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId,
null,
"UPPER(" + ContactsContract.Contacts.DISPLAY_NAME
+ ") ASC");
while (phones.moveToNext()) {
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Map<String, String> NamePhoneType = new HashMap<String, String>();
NamePhoneType.put("Name", contactName);
NamePhoneType.put("Phone", phoneNumber);
list.add(NamePhoneType);
aa=contactName;
bb=phoneNumber;
}
phones.close();
}
}
people.close();
return list;
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
CheckBox checkbox = (CheckBox) v.findViewById(R.id.checkBox1);
if (checkbox.isChecked() == false) {
checkbox.setChecked(true);
int aaa=l.getCheckedItemPosition();
} else {
checkbox.setChecked(false);
}
}
xml代码
主要 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5.0px"
android:paddingLeft="5.0px"
android:paddingTop="5.0px" >
<TextView
android:id="@+id/txtContactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15.0dip"
android:layout_toLeftOf="@+id/checkBox1"
android:layout_alignParentLeft="true"
android:text="Medium Text"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="@+id/txtContactNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtContactName"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15.0dip"
android:layout_toLeftOf="@+id/checkBox1"
android:text="Small Text"
android:textAppearance="?android:textAppearanceSmall" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
</RelativeLayout>
活动控制 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected" />
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:clickable="true"
android:layout_below="@+id/btnShow" >
</ListView>
</RelativeLayout>