I have tried using this code but i'm not able to retrieve the data from onLoadFinished method, i have got cursor.columnCount=7 and cursor.getCount=0. Whenever i'm trying to retrieve data like- cursor.getString(cursor.getColumnIndex(projection[0])) i'm getting error saying that size 0 and index 0.

private static final String[] PROJECTION =
        {
                ContactsContract.Data.PHOTO_THUMBNAIL_URI,
                ContactsContract.Data.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone._ID,
                ContactsContract.CommonDataKinds.Email.ADDRESS,
                ContactsContract.CommonDataKinds.Email.TYPE,
                ContactsContract.CommonDataKinds.Email.LABEL,
                ContactsContract.CommonDataKinds.Email.ADDRESS
        };
private static final int DETAILS_QUERY_ID = 0;

private static final String SELECTION = ContactsContract.Data.LOOKUP_KEY + " = ?";
private String[] selectionArgs = { "" };
String lookupKey;

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    lookupKey=getArguments().getString("LOOKUPKEY");
    getLoaderManager().initLoader(DETAILS_QUERY_ID, null, this);

}


@Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle args) {
    selectionArgs[0] = lookupKey;

    return new CursorLoader(
            getActivity(),
            ContactsContract.Data.CONTENT_URI,
            PROJECTION,
            SELECTION,
            selectionArgs,
            null
    );

}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

}

@Override
public void onLoaderReset(@NonNull Loader<Cursor> loader) {

}

}

1 Answers

0
marmor On Best Solutions

i'll phrase this an as answer: your code seems to be correct, but the passed value is not instead of passing a lookupKey you're passing the contact name to the fragment.

change private static final int CONTACT_KEY_INDEX to 3, not 1. this should represent the index of LOOKUP_KEY in your projection array