2012-08-08 199 views
1

我想在列表視圖中顯示聯繫人編號以及聯繫人姓名。 我做...:如何從聯繫人編號中讀取聯繫人姓名?

public String DisplayName(String number) { 
    Uri uri = Uri.withAppendedPath(
      ContactsContract.PhoneLookup.CONTENT_FILTER_URI, 
      Uri.encode(number)); 
    String name = "?"; 

    ContentResolver contentResolver = getContentResolver(); 
    Cursor contactLookup = contentResolver.query(uri, new String[] { 
      BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME }, 
      null, null, null); 

    try { 
     if (contactLookup != null && contactLookup.getCount() > 0) { 
      contactLookup.moveToNext(); 
      name = contactLookup.getString(contactLookup 
        .getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); 
      // String contactId = 
      // contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID)); 
     } 
    } finally { 
     if (contactLookup != null) { 
      contactLookup.close(); 
     } 
    } 

    return name; 
} 

,但我的應用程序不斷強制關閉,我不知道爲什麼?我已包括許可也... 「」

任何猜測?爲什麼

+0

讀取[此](http://stackoverflow.com/questions/3572463/what- is-context-in-android)有關上下文的詳細信息... – 2012-08-08 11:20:32

回答

4

使用此2種方法:

/* 
* Returns contact's id 
*/ 
private String getContactId(String phoneNumber, Context context) { 
    ContentResolver mResolver = context.getContentResolver(); 

    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
      Uri.encode(phoneNumber)); 

    Cursor cursor = mResolver.query(uri, new String[] { 
      PhoneLookup.DISPLAY_NAME, PhoneLookup._ID }, null, null, null); 

    String contactId = ""; 

    if (cursor.moveToFirst()) { 
     do { 
      contactId = cursor.getString(cursor 
        .getColumnIndex(PhoneLookup._ID)); 
     } while (cursor.moveToNext()); 
    } 

    cursor.close(); 
    cursor = null; 
    return contactId; 
} 

/* 
* Returns contact's name 
*/ 
private String getContactName(String contactId, Context context) { 

    String[] projection = new String[] { Contacts.DISPLAY_NAME }; 
    Cursor cursor = mResolver.query(Contacts.CONTENT_URI, projection, 
      Contacts._ID + "=?", new String[] { contactId }, null); 
    String name = ""; 
    if (cursor.moveToFirst()) { 
     name = cursor.getString(0); 
    } 

    cursor.close(); 
    cursor = null; 
    return name; 
} 
+0

thanx亞歷克斯,我是新的android ...第一個函數中的這個上下文是什麼?,對不起,如果這是一個愚蠢的問題。 – 2012-08-08 11:08:13

+0

你可以使用你的活動作爲上下文 – 2012-08-08 11:11:11

+0

mResolver?這是什麼......它顯示錯誤在這裏 – 2012-08-08 11:45:21

0

使用下面的代碼:

public void readContacts(){ 
    ContentResolver cr = parent.getContentResolver(); 
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
      null, null, null, null); 

    if (cur.getCount() > 0) { 
     while (cur.moveToNext()) { 
      Contact tempcontact=new Contact(); 
      contactsList.add(tempcontact); 
      String id=cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
      String name=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      tempcontact.setId(cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID))); 
      tempcontact.setName(name); 
      if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
       Log.e("DATA","name : " + name + ", ID : " + id); 

       // get the phone number 
       Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
             ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
             new String[]{id}, null); 
       while (pCur.moveToNext()) { 
        if(pCur.getString(
          pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))!=null) 
        { 
        tempcontact.getPhonenumber().add(pCur.getString(
          pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 
        } 
        // System.out.println("phone" + phone); 
        // Log.e("phone",phone); 
       } 
       pCur.close(); 


       // get email and type 

       Cursor emailCur = cr.query(
         ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
         new String[]{id}, null); 
       while (emailCur.moveToNext()) { 
        // This would allow you get several email addresses 
         // if the email addresses were stored in an array 
        String email = emailCur.getString(
           emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
        tempcontact.getEmail().add(emailCur.getString(
           emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA))); 
        String emailType = emailCur.getString(
           emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 

        Log.e("Data","Email " + email + " Email Type : " + emailType); 
       } 
       emailCur.close(); 


       String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
       String[] addrWhereParams = new String[]{id, 
        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE}; 
       Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, 
          null, null, null, null); 
       while(addrCur.moveToNext()) { 
        tempcontact.setPoBox((addrCur.getString(
           addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX)))); 
        tempcontact.setPoBox(addrCur.getString(
           addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET))); 
        tempcontact.setCity(addrCur.getString(
           addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY))); 
        tempcontact.setRegion(addrCur.getString(
           addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION))); 
        tempcontact.setPostalCode(addrCur.getString(
           addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE))); 
        tempcontact.setCountry((addrCur.getString(
           addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY)))); 
        tempcontact.setType(addrCur.getString(
           addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE))); 

        // Do something with these.... 

       } 
       addrCur.close(); 



       String[] projection = {ContactsContract.CommonDataKinds.Photo.PHOTO}; 
       Uri uri = (Uri)ContactsContract.Data.CONTENT_URI; 
       String where = ContactsContract.Data.MIMETYPE 
         + "=" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + " AND " 
         + ContactsContract.Data.CONTACT_ID + " = " + id; 
       Cursor cursor = parent.getContentResolver().query(uri, projection, null, null, null); 

       if(cursor!=null&&cursor.moveToFirst()){ 
        do{ 
         byte[] photoData = cursor.getBlob(0); 
         tempcontact.setPhoto(photoData); 

         //Do whatever with your photo here... 
        }while(cursor.moveToNext()); 
       } 



      } 
     } 

    } 
    } 


public class Contact { 
String id; 
String name; 
ArrayList<String> phonenumber; 
ArrayList<String> email; 
String poBox; 
String street; 
String city; 
String state; 
String postalCode; 
String country; 
String type; 
String region; 
byte[] photo; 
public byte[] getPhoto() { 
    return photo; 
} 

public Contact() { 
// TODO Auto-generated constructor stub 
phonenumber=new ArrayList<String>(); 
email=new ArrayList<String>(); 
} 

public void setPhoto(byte[] photo) { 
    this.photo = photo; 
} 

public String getPoBox() { 
    return poBox; 
} 

public void setPoBox(String poBox) { 
    this.poBox = poBox; 
} 

public String getStreet() { 
    return street; 
} 

public void setStreet(String street) { 
    this.street = street; 
} 

public String getCity() { 
    return city; 
} 

public void setCity(String city) { 
    this.city = city; 
} 

public String getState() { 
    return state; 
} 

public void setState(String state) { 
    this.state = state; 
} 

public String getPostalCode() { 
    return postalCode; 
} 

public void setPostalCode(String postalCode) { 
    this.postalCode = postalCode; 
} 

public String getCountry() { 
    return country; 
} 

public void setCountry(String country) { 
    this.country = country; 
} 

public String getType() { 
    return type; 
} 

public void setType(String type) { 
    this.type = type; 
} 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public ArrayList<String> getPhonenumber() { 
    return phonenumber; 
} 

public void setPhonenumber(ArrayList<String> phonenumber) { 
    this.phonenumber = phonenumber; 
} 

public ArrayList<String> getEmail() { 
    return email; 
} 

public void setEmail(ArrayList<String> email) { 
    this.email = email; 
} 

public String getRegion() { 
    return region; 
} 

public void setRegion(String region) { 
    this.region = region; 
} 

} 
0
Cursor phones= getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); 
while (phones.moveToNext()) 
{ 

    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

} 
phones.close(); 
相關問題