【发布时间】:2015-10-14 06:42:13
【问题描述】:
我的代码基于 Android Studio 提供的登录示例。该示例包含使用与设备的ContactsContract.Profile 联系人相关的电子邮件地址填充AutoCompleteTextView 的代码。即手机的主人,我。
我需要继续使用LoaderCallbacks 接口方法-onCreateLoader() 和onLoaderFinished()。
我想获取联系人的其他详细信息,例如:
- 电话号码
- 名字
- 姓氏
为了实现这一点,我尝试向示例中定义的ProfileQuery 接口添加额外的字段(可以正常获取电子邮件地址):
private interface ProfileQuery {
String[] PROJECTION = {
// these fields as per Android Studio sample
ContactsContract.CommonDataKinds.Email.ADDRESS,
ContactsContract.CommonDataKinds.Email.IS_PRIMARY,
// these fields added by me
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME
};
}
我修改了onCreateLoader()方法,去掉了样本的WHERE子句,希望得到额外的数据:
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new CursorLoader(this,
// Retrieve data rows for the device user's 'profile' contact.
Uri.withAppendedPath(ContactsContract.Profile.CONTENT_URI,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY), ProfileQuery.PROJECTION,
// select all fields
null, null,
// Show primary email addresses first. Note that there won't be
// a primary email address if the user hasn't specified one.
ContactsContract.Contacts.Data.IS_PRIMARY + " DESC");
}
不管怎样,目前我的onLoadFinished() 只是将接收到的数据记录下来:
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Log.d("xxx", cursor.getString(0) + cursor.getString(1) + cursor.getString(2) + cursor.getString(3) + cursor.getString(4));
cursor.moveToNext();
}
}
我希望每个光标行都为我提供与个人资料联系人相关的完整数据集。相反,我从那个联系人那里得到了看似随机的字段。
我的CursorLoader 构造显然是错误的,但我不知道如何解决。
如何从我的个人资料联系人那里获得以下详细信息:
- 电子邮件地址
- 电话号码
- 名字
- 姓氏?
【问题讨论】:
标签: android android-contacts contactscontract