1

可能重复:
如何为填充的列表视图中的每个联系人链接一个复选框?

我是初学者android

我想获取电话联系人,然后将它们放入listview 带有复选框的复选框中,以便用户可以选择多个联系人,然后获取选定的联系人。

有没有tutorial呢????

谢谢

4

2 回答 2

3

这是使用复选框获取所有联系人的代码:

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

if (cur.getCount() > 0) {
  while (cur.moveToNext()) {
    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
      // This inner cursor is for contacts that have multiple numbers.
      Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
      while (pCur.moveToNext()) {
        phoneContactList.add(name);
        Log.i("Contact List", name);
      }
      pCur.close();
    }
  }

  Collections.sort(phoneContactList);
  cnt = phoneContactList.size();

  listView.setAdapter(new ArrayAdapter<String>(this, R.drawable.multiple_contact_selector, phoneContactList));
  listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

}
cur.close();
于 2012-09-18T04:40:42.170 回答
2

我认为这可以帮助您: 自定义 Android ListView 以读取手机联系人

首先检查这个

1)在清单文件中添加权限。

<uses-permission android:name="android.permission.READ_CONTACTS"/>
于 2012-09-18T04:28:10.613 回答