2012-06-07 45 views
0
String exactname = "Joe Blogs" 

我搜索的方式直接打開使用DISPLAY_NAME接觸的不是_ID接觸高低。打開接觸意圖DISPLAY_NAME

編輯:喬博客絕對是一個獨特的條目。

this answer

Intent intent = new Intent(Intent.ACTION_VIEW); 
    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactID)); 
    intent.setData(uri); 
    context.startActivity(intent); 

我一定要查詢喬博客的ID之前,我可以打開他的意圖類似上述的接觸?或者我(希望)錯過了什麼?

在此先感謝。

+0

如果您有兩個具有相同名稱的聯繫人會怎麼樣。這就是爲什麼android需要id.because id是唯一的。 –

+0

我的意思是在喬博客絕對是獨一無二的問題中加入。 – brandall

回答

1

是的,你需要的東西,如查詢數據庫,

Cursor cur = getContentResolver().query(CONTENT_URI, null, DISPLAY_NAME + "=?", new String[] { "Joe Blogs" }, null); 

然後檢索由Cursor與返回的行,

if (!cur.moveToFirst()) { 
    // Then the cursor isn't empty. Get the contact's id. 
    contactId = cur.getInt(cur.getColumnIndex(CONTACT_ID)); 
} 

這將返回第一個關聯的聯繫人ID記錄在Cursor

+0

謝謝你的回答。在你的例子中,我可以用'exactname'替換新的String [] {「Joe Blogs」} - 我即將爲自己測試。另外,是否沒有直接查找方法,例如getID(exactname)?謝謝。 – brandall

+0

不,並且沒有這種方法的原因是因爲您正在收集來自* database *,而不是* class *的數據。這是理解的重要概念。 –

+0

再次感謝您。標記你的答案是正確的 – brandall