如何在Android上获取联系人的名字和姓氏列表?

时间:2012-10-22 20:23:59

标签: java android eclipse android-contacts

  

可能重复:
  How to read contacts on Android 2.0

我只想在Android中的设备联系人列表中使用List<String>名字+姓氏。应该是非常简单的东西,但我找不到任何基本代码。

只是寻找:

Steve Smith
Joe Shmo
Blah Davis

等。全部来自用户手机上的联系人。我已经添加了:

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

1 个答案:

答案 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;
}