我想从Android电话簿中选择所有联系人,并希望使用cursor和listview将它们存储在列表中。这样我就可以在列表中放入复选框,从电话簿中选择多个联系人。
我怎样才能实现这一目标?
提前致谢
答案 0 :(得分:1)
首先,你必须进行自定义布局
<LinearLayout>
<TextView ...../>
<CheckBox....../>
</LinearLayout>
现在在java代码中使用ContactsContracts ...
代码 Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + NAME + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
// put Display name in textview id which is used in Linear Layout...
这是你可以参考的链接.. http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/