【问题标题】:How to add Android Contacts into a listview with a check box [duplicate]如何使用复选框将Android联系人添加到列表视图中[重复]
【发布时间】:2012-09-10 07:47:39
【问题描述】:

可能重复:
How do I link a checkbox for every contact in populated listview?

我是android 的初学者。

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

有没有tutorial 呢????

谢谢

【问题讨论】:

标签: android android-listview android-contacts android-checkbox


【解决方案1】:

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

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();

【讨论】:

  • 并在清单文件中添加权限为:&lt;uses-permission android:name="android.permission.READ_CONTACTS"/&gt;
【解决方案2】:

我认为这可以帮助你:Custom Android ListView to read phone contacts

首先检查一下

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

<uses-permission android:name="android.permission.READ_CONTACTS"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2021-11-17
    • 2015-09-06
    • 1970-01-01
    相关资源
    最近更新 更多