How to add Android Contacts into a listview with a check box [duplicate]

眉间皱痕 提交于 2019-12-08 07:44:54

问题


Possible Duplicate:
How do I link a checkbox for every contact in populated listview?

i m a beginner in android.

i want to get the phone contacts and then put them into a listview with a checkbox so that user can select more then one contact and then get the selected contacts.

is there any tutorial for that????

thanks


回答1:


here is the code to get all contacts with checkbox :

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



回答2:


I think this can help you : Custom Android ListView to read phone contacts .

First of all check this

1) Add permission in the manifest file.

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


来源:https://stackoverflow.com/questions/12470363/how-to-add-android-contacts-into-a-listview-with-a-check-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!