使用Loader的Android联系人电话号码无效列数据1 [英] Android Contact phone number using Loader Invalid column data1
问题描述
我以前曾经使用内容解析器来获取联系人列表及其详细信息,但仍然可以使用,我想尝试使用Loader方法在后台查询Content Provider。我在这里查看了文档和示例代码:
I used to use content resolver previously to fetch Contact list and their details while that still works I wanted to try the Loader method which queries the Content Provider in the background. I have looked at the documentation and sample code here :
http://developer.android.com/training/contacts-provider/retrieve-details.html
虽然我可以毫无问题地显示联系人列表,但是我仍然无法通过单击特定联系人的电话号码并将其显示在详细信息屏幕上来检索该电话号码。上面的演示在详细信息屏幕上显示了用户的地址,我什至尝试对其进行修改。我已将内容URI从联系人更改为ContactsContract.CommonDataKinds.Phone,但我不断得到
While I was able to display a list of Contacts without any problems, I am stuck at retrieving Phone Number of a particular contact on clicking it and showing it on a detail screen. The demo above shows address of the user on the detail screen, I even tried modifying it. I have changed the content URI from Contacts to ContactsContract.CommonDataKinds.Phone but I keep getting
java.lang.IllegalArgumentException: Invalid column data1
互联网上没有一个详细的教程来说明它是如何完成的。在座的人能和kind可亲吗?阅读其他各种Stackoverflow问题,我很确定这与2个不同的CONTENT URI有关,但我可能还是错了。任何帮助或指针将不胜感激。谢谢
There is not a single detailed tutorial on the internet showing how it is done. Can someone here be kind enough to shed some light? Reading various other Stackoverflow questions I am oretty sure it has something to do with the 2 different CONTENT URI but again I could be wrong. Any help or pointers would be really appreciated. Thanks
我已经看过的一些SO问题:
Few of the SO Questions I have already looked at :
它们都不能解决我的问题。这是失败的相关代码:
None of them solve my problem. Here is the relevant code where it fails :
final static String[] PROJECTION = {
Contacts._ID,
Utils.hasHoneycomb() ? ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY : ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
//ContactsContract.CommonDataKinds.Phone.NUMBER,
};
代码可以正常工作,但是当我删除注释以获取电话号码失败时
The code works just fine, but the moment I remove the comment to fetch Phone Number it fails
推荐答案
这是我在(工作)代码中实际所做的摘录。
This is an excerpt of what I actually do in my (working) code.
这些是我相关的导入内容:
These are my relevant imports:
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
鉴于id是一个字符串(代表数字,例如 1, 20 、。 ..)
Given that id is a string (representing a number, like "1", "20", ...)
// Query for phone numbers for the selected contact id
final Cursor cur = getContentResolver().query
(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + "=?",
new String[] {id}, null
);
final int phoneIdx = cur.getColumnIndex(Phone.NUMBER);
final int phoneType = cur.getColumnIndex(Phone.TYPE);
// ...
if(cur.moveToFirst())
{
final String name =
cur.getString
(
cur.getColumnIndexOrThrow
(
ContactsContract.Contacts.DISPLAY_NAME
)
);
}
希望它能使您走上正确的道路
Hope it puts you on the right path
这篇关于使用Loader的Android联系人电话号码无效列数据1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!