在Android上获取所有联系人姓名和号码作为字符串列表

use*_*572 5 android contacts android-contentprovider android-contacts

我找到了一个漂亮的联系人列表布局:https://github.com/thehung111/ContactListView

但是联系人是硬编码的.所以我需要获取手机联系人并填写联系人列表.

这是我尝试过的:

public class ExampleDataSource  {

public static List<ContactItemInterface> getSampleContactList(){
     List<ContactItemInterface>  list = new  ArrayList<ContactItemInterface> ();


     Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                         ContactsContract.CommonDataKinds.Phone.NUMBER};
     Cursor people = getContentResolver().query(uri, projection, null, null, null);

     int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
     int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

     people.moveToFirst();
     do {
         String name   = people.getString(indexName);
         String number = people.getString(indexNumber);
         list.add(new ExampleContactItem(name , number ) );

     } while (people.moveToNext());

     /* Example inputs for contact list

     list.add(new ExampleContactItem("Lizbeth" , "Lizbeth Crockett" ) );
     list.add(new ExampleContactItem("Lizbeth" , "Lizbeth Crockett" ) );
     list.add(new ExampleContactItem("Zachery" , "Zachery Loranger" ) );
     list.add(new ExampleContactItem("Vada" , "Vada Winegar" ) );
     list.add(new ExampleContactItem("Essie" , "Essie Pass" ) );

    */   
     return list;
}
Run Code Online (Sandbox Code Playgroud)

}

我在getContentResolver()上遇到错误,并尝试将类扩展到Application等.到目前为止没有运气.

因此,主要问题是如何在Android上获取包含姓名和电话号码的列表作为字符串列表.

Rah*_*952 0

您是否在清单中声明了以下许可?如果没有,请声明。

  <uses-permission android:name="android.permission.READ_CONTACTS" />
Run Code Online (Sandbox Code Playgroud)