gpt4 book ai didi

android - 如何在android中获取联系网站信息?

转载 作者:行者123 更新时间:2023-11-29 19:22:17 24 4
gpt4 key购买 nike

我尝试获取联系方式。我可以从 Phone.CONTENT_URI 获取 DISPLAY_NAME、Phone.DATA。我使用以下代码:

String urlData = "";
final String[] projection = new String[] {
ContactsContract.CommonDataKinds.Website.URL,
ContactsContract.CommonDataKinds.Website.TYPE
};
final Cursor contactData = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=" + contactId,null, null);

if (contactData.moveToNext()) {

int urlColumnIndex = contactData.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL);
String url = contactData.getString(urlColumnIndex);
String urlType = contactData.getString(contactData.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE));
urlData += url + " " + urlType;
return urlData;
}

它返回 display_name 的值。我不明白为什么我无法获取 url 数据。

我该怎么做?

编辑

我有 Contacts._ID。

String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

我使用下面的函数来获取联系 url 地址。

private void getURLById(String id, ContentResolver cr) {
Log.i("@@getURLById", ".");

final String[] projection = new String[] {
ContactsContract.CommonDataKinds.Website.URL,
ContactsContract.CommonDataKinds.Website.TYPE
};

String selection = ContactsContract.Data._ID + " = " + id + " AND " + ContactsContract.Contacts.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'";

final Cursor websites = getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, null, null);

Log.e("Count ", " " + websites.getCount());

计数返回零。

最佳答案

保留对数据表的查询,并添加以下选择:Data.CONTACT_ID + "= "+ contactId + "and "+ Data.MIMETYPE + "= '"+ CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'"

这将只选择网站类型的行。

代码:

final String[] projection = new String[] {
ContactsContract.CommonDataKinds.Website.URL,
ContactsContract.CommonDataKinds.Website.TYPE
};
String selection = Data.CONTACT_ID + " = " + contactId + " AND " + Data.MIMETYPE + " = '" + CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'";

final Cursor contactData = getContentResolver().query(Data.CONTENT_URI, projection, selection, null, null);

更新

Contacts._ID 不是 Data._ID,将您的选择更改为:

String selection = ContactsContract.Data.CONTACT_ID + " = " + id + " AND " + ContactsContract.Contacts.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'";

关于android - 如何在android中获取联系网站信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42174458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com