I am basing my code on the Login sample supplied with Android Studio. That sample contains code to populate an AutoCompleteTextView
with email addresses related to the device's ContactsContract.Profile
contact. i.e. the phone's owner, me.
I need to keep using the LoaderCallbacks
interface methods - onCreateLoader()
and onLoaderFinished()
.
I want to fetch extra details on the contact like:
- phone number
- given name
- family name
To achieve this, I have tried adding extra fields to the ProfileQuery
interface defined in the sample (that works correctly to fetch email address):
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
};
}
I have modified the onCreateLoader()
method to remove the sample's WHERE clause in the hope to get the extra data:
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");
}
For what it is worth, at the moment my onLoadFinished()
just logs the received data out:
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();
}
}
I would like each cursor row to give me a complete set of data relating to the Profile contact. Instead, I am getting seemingly random fields from that contact.
My CursorLoader
construction is clearly wrong, but I do not know how to fix that.
How can I get the following details from my Profile contact:
- email address
- phone number
- given name
- family name?