【问题标题】:Get contact details from contact id in Android从 Android 中的联系人 ID 获取联系人详细信息
【发布时间】:2012-02-21 03:40:42
【问题描述】:

我想从联系人列表视图中获取多个联系人详细信息。我有这个代码:

list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
        //HERE I WANT TO GET CONTACT DETAILS FROM THE ID PARAMETER

        Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, Uri.encode(id));
        Cursor c = getContentResolver().query(lookupUri, new String[]{Contacts.DISPLAY_NAME}, null,null,null);
        try {
              c.moveToFirst();
              String displayName = c.getString(0);
        } finally {
              c.close();
        }

}

但是我得到了这个异常:IllegalArgumentException, Invalid lookup id (当我从游标调用查询方法时)。 所以我不知道如何从项目列表中获取有效的查找 ID。

有什么想法吗?谢谢!

【问题讨论】:

    标签: android list lookup contacts


    【解决方案1】:

    这里的id 表示您要获取其联系方式的contact id

    获取特定contact id 的电话号码的简单代码如下:

                        // Build the Uri to query to table
                        Uri myPhoneUri = Uri.withAppendedPath(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
    
                        // Query the table
                        Cursor phoneCursor = managedQuery(
                                myPhoneUri, null, null, null, null);
    
                        // Get the phone numbers from the contact
                        for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) {
    
                            // Get a phone number
                            String phoneNumber = phoneCursor.getString(phoneCursor
                                    .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
                            sb.append("Phone: " + phoneNumber + "\n");
                        }
                    }
    

    所以,从你的问题来看,我不得不怀疑你在你的 uri 中唱的 id 参数,只是清楚一点,在我的例子中,id 是字符串类型......

    希望你能理解。

    更新: Uri.encode(id) 而不是只传递 字符串格式的 id

    谢谢..

    【讨论】:

      【解决方案2】:

      因为managedQuery 已被弃用,您也可以使用

      Cursor phoneCursor = getContentResolver().query(myPhoneUri, null, null, null, null);
      

      【讨论】:

        【解决方案3】:
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        int columnIndex = arg2.getColumnIndex(ContactsContract.Contacts._ID);
        
        private void retriveContactImage(ImageView imageView, int columnIndex) {
            Bitmap photo = null;
            try {
                InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(),
                        ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(columnIndex)));
                if (inputStream != null) {
                    photo = BitmapFactory.decodeStream(inputStream);
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            imageView.setImageBitmap(photo);
        }
        
        private ArrayList<String> retrieveContactNumbers(TextView textView, long contactId) {
            ArrayList<String> phoneNum = new ArrayList<String>();
            Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactId + "" }, null);
            if (cursor.getCount() >= 1) {
                while (cursor.moveToNext()) {
                    // store the numbers in an array
                    String str = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    if (str != null && str.trim().length() > 0) {
                        phoneNum.add(str);
                    }
                }
            }
            cursor.close();
            textView.setText(phoneNum.get(0));
            return phoneNum;
        }
        
        private void retrieveContactName(TextView textView, long contactId) {
            String contactName = null;
        
            Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, new String[] { ContactsContract.Contacts.DISPLAY_NAME },
                    ContactsContract.Contacts._ID + " = ?", new String[] { String.valueOf(contactId) }, null);
        
            if (cursor.moveToFirst()) {
                contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            }
            cursor.close();
            textView.setText(contactName);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-12-21
          • 1970-01-01
          • 2012-01-11
          • 1970-01-01
          相关资源
          最近更新 更多