1

我使用下面的代码,但我只需要联系人姓名,此代码从联系人、Facebook、Nimbuzz 和....中检索所有数据。Cursor cursor = getContentResolver().query(

            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    while (cursor.moveToNext()) {

        //numberPhone = 0;
        contactId = cursor.getString(cursor
                .getColumnIndex(ContactsContract.Contacts._ID));



        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
                Long.parseLong(contactId));
        Uri dataUri = Uri.withAppendedPath(contactUri,
                Contacts.Data.CONTENT_DIRECTORY);

        try {
            Cursor nameCursor = getContentResolver()
                    .query(dataUri,null,Data.MIMETYPE + "=?",new String[] { StructuredName.CONTENT_ITEM_TYPE }, null);
            int indexDisplayName = nameCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
            nameCursor.moveToFirst();
            do {

                String firstName = nameCursor.getString(nameCursor
                        .getColumnIndex(Data.DATA2));
                String lastName = nameCursor.getString(nameCursor
                        .getColumnIndex(Data.DATA3));
                Arrayename.add(firstName+" "+lastName);
                //String display = nameCursor.getString(indexDisplayName);





                //Log.d("display name", "display name is: " + display);

            } while (nameCursor.moveToNext());
            nameCursor.close();
        } catch (Exception e) {
            Toast.makeText(this, "Error Happend While Reading Names",
                    Toast.LENGTH_SHORT).show();
        }
    }

如何仅获取联系人数据?

4

1 回答 1

0

如果我在下面正确地理解了您,那么您需要的是:

创建一个用于显示结果的 xml 文件,该文件将获得:

  1. 创建一个按钮将触发意图:
  2. 创建一个文本视图或编辑文本(我使用了编辑文本)

在按钮单击时启动类似调用 startActivityforresult(its args....) 的意图:

 final static int CONTACTS_RESULT = 3;

 Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
 startActivityForResult(intent, CONTACTS_RESULT);

然后处理回调的结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {

    case CONTACTS_RESULT:

        if (resultCode == RESULT_OK) {

            Uri returnedData = data.getData();
            id = returnedData.getLastPathSegment();

            name = retriveName(data);
            etName.setText(name);
          }

         break;
              }

       }

// 该方法用于获取联系人的姓名并将其值设置为返回值,方法是在

private String retriveName(Intent data) 
           {
    String name = null;
    Uri returnedData = data.getData();
    String id = returnedData.getLastPathSegment();

    // Cursor for name

    Cursor cursor = getContentResolver().query(returnedData, null, null,
            null, null);

    if (cursor.moveToFirst()) 
                               {

        name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));


    }
    return name;

}

看看这是否适合你....

编辑:您是否尝试过将 Contacts.DISPLAY_NAME 替换为以下两个:

  1. 联系人。DISPLAY_NAME_PRIMARY
  2. 联系人。DISPLAY_NAME_SOURCE
于 2013-08-11T06:19:23.507 回答