2012-01-28 3 views
4

Je souhaite obtenir plusieurs détails de contact à partir d'une vue de liste de contacts. J'ai ce code:Détails de contact de l'ID de contact dans Android

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(); 
     } 

} 

Mais je reçois cette exception: IllegalArgumentException, id de recherche non valide (quand je l'appelle méthode d'interrogation du curseur). Donc, je ne sais pas comment obtenir un identifiant de recherche valide à partir de la liste des éléments.

Une idée? Merci!

Répondre

9

Ici id moyens, un contact id pour lequel vous voulez chercher les coordonnées,

Le code simplement pour obtenir le numéro de téléphone pour un contact id particulier est comme,

    // 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"); 
        } 
       } 

Alors, de votre question Je dois douter pour le paramètre id qui chantez dans votre uri juste effacer cela, aussi dans mon exemple l'id est le type de chaîne ...

Espérons que vous le comprendrez.

Mise à jour:Uri.encode(id) au lieu de simplement passer id au format chaîne.

Merci ..

3

Parce que managedQuery est vous dépréciée pouvez également utiliser

Cursor phoneCursor = getContentResolver().query(myPhoneUri, null, null, null, null); 
0
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); 
} 
Questions connexes