如何获取android中的联系网站信息?
How to get contact website information in android?
我尝试获取联系方式。我可以从 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;
}
它returns的值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());
计数 returns 零。
保留数据 table 的查询,并添加以下 selection:
Data.CONTACT_ID + " = " + contactId + " and " + Data.MIMETYPE + " = '" + CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'"
这将 select 只有网站类型的行。
代码:
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
,将你的 selection 更改为:
String selection = ContactsContract.Data.CONTACT_ID + " = " + id + " AND " + ContactsContract.Contacts.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'";
我尝试获取联系方式。我可以从 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;
}
它returns的值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());
计数 returns 零。
保留数据 table 的查询,并添加以下 selection:
Data.CONTACT_ID + " = " + contactId + " and " + Data.MIMETYPE + " = '" + CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'"
这将 select 只有网站类型的行。
代码:
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
,将你的 selection 更改为:
String selection = ContactsContract.Data.CONTACT_ID + " = " + id + " AND " + ContactsContract.Contacts.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE + "'";