我发现这个漂亮的联系人列表布局: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;
}
}
我在 getContentResolver() 上遇到错误,并试图将类扩展到应用程序等。到目前为止还没有运气。
所以主要问题是如何在Android上获取一个包含姓名和电话号码的列表作为字符串列表。