我只想在Android中的设备联系人列表中使用List<String>
名字+姓氏。应该是非常简单的东西,但我找不到任何基本代码。
只是寻找:
Steve Smith
Joe Shmo
Blah Davis
等。全部来自用户手机上的联系人。我已经添加了:
<uses-permission android:name="android.permission.READ_CONTACTS" />
答案 0 :(得分:1)
public List<String> getFullContactName(){
List<String> name = new List<String>();
String[] projection = new String[] {Data.DATA2, Data.DATA3};
String where = Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'";
Uri uri = Data.CONTENT_URI;
ContentResolver contentResolver = context.getContentResolver();
// Make the query.
Cursor cursor = contentResolver.query(uri,
projection, // Which columns to return
where, // Which rows to return
null, // Selection arguments (none)
// Put the results in ascending order by name
null);
String firstName, LastName;
while (cursor.moveToNext()) {
firstName = cursor.getString(cursor.getColumnIndex(Data.DATA2));
lastName = cursor.getString(cursor.getColumnIndex(Data.DATA3));
name.add(firstName + " " + lastName);
}
cursor.close();
cursor = null;
return name;
}