簡體   English   中英

如何匹配Android內容提供商的DISPLAY_NAME和Phone.NUMBER閱讀聯系人

[英]How to match DISPLAY_NAME and Phone.NUMBER in android content provider wile reading contacts

我一直在練習閱讀android的聯系人,我正在獲取聯系人和數字,但他們排列不好。 我的意思是指定的名稱不是數字的所有者。 這是我的代碼:

String [] projection= new String[]{ContactsContract.Contacts.DISPLAY_NAME,};
    String [] phoneProjection= new String [] {Phone.NUMBER};


     ContentResolver crInstance=getContentResolver();

    final  Cursor name=crInstance.query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null);
    final Cursor phone=crInstance.query(Phone.CONTENT_URI, phoneProjection, null, null, null);
    contactview = (TextView) findViewById(R.id.contactview);
    name.moveToFirst();
    phone.moveToFirst();
    while (!name.isAfterLast()&&!phone.isAfterLast()){
        String pname=name.getString(name.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));



        contactview.append("Name:");
        contactview.append(pname);
        contactview.append("\n");


        final int contactNoColIndex= phone.getColumnIndex(Phone.NUMBER);

        String pnumber=phone.getString(contactNoColIndex);

        contactview.append("Phone:");
        contactview.append(pnumber);
        contactview.append("\n");
        contactview.append("\n");

        name.moveToNext();
        phone.moveToNext();
    }
    name.close();
    phone.close();

請幫忙

這是一個簡單的方法:

 ContentResolver resolver = getContentResolver();
 Cursor cursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

    if (cursor.moveToFirst()) {

        String name;
        String phone;

        while (cursor.moveToNext()) {
            name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            // do what you want with the name and the phone number 

        }

    } else {
        Log.v(LOG_TAG, "Cursor is empty");
    }

cursor.close();

不要忘記在清單中添加權限

此代碼基本上先獲取Number,然后使用它來搜索聯系人姓名。

`

Cursor c;
c= mydb.query("sms", null, null, null, null, null, null);
while (c.moveToNext()) {
String name = null;
String sender=c.getString(c.getColumnIndex("phoneNo"));
String time=c.getString(c.getColumnIndex("smstype"));
String smsbody=c.getString(c.getColumnIndex("message"));
String[] projection = new String[]{
ContactsContract.PhoneLookup.DISPLAY_NAME};
Uri contacturi= Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(sender));
ContentResolver cr = getContentResolver();          
Cursor crc= cr.query(contacturi, projection, null, null, null);
if(crc.moveToNext()){   name=crc.getString(crc.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));  }   else{
name ="Unknown";
}
String senderid= name+" "+ sender;`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM