I am trying to get Number and email Address from my phone's contact book. For number the content uri is ContactsContract.Commomdatakinds.Phone.CONTENT_URI and for Email is ContactsContract.Commomdatakinds.Email.CONTENT_URI so now i want to pass these two URI to cursor loader and get result from these but the problem is cursor loader can return only one cursor at a time. so can anyone help me out solving this problem the code is given below
public class Cursor extends Fragment implements LoaderManager.LoaderCallbacks<android.database.Cursor>
{
ListView listView;
ArrayList<String> contact_id;
int position=0;
private static final int URL_LOADER = 0;
private static final int NAME_URL = 1;
private static final int NUMBER_URL = 2;
private static final int EMAIL_URL = 3;
public String[] mFromColumns = {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Email.ADDRESS
};
public int[] mToFields = {
R.id.text1,
R.id.text2,
R.id.text3,
R.id.text4
};
SimpleCursorAdapter mAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.cursor,container,false);
getLoaderManager().initLoader(NUMBER_URL, null, this);
getLoaderManager().initLoader(EMAIL_URL, null, this);
listView = (ListView) v.findViewById(R.id.list_item);
mAdapter =
new SimpleCursorAdapter(
getContext(), // Current context
R.layout.textview, // Layout for a single row
null, // No Cursor yet
mFromColumns, // Cursor columns to use
mToFields, // Layout fields to use
0 // No flags
);
// Sets the adapter for the view
listView.setAdapter(mAdapter);
return v;
}
@Override
public Loader<android.database.Cursor> onCreateLoader(int i, Bundle bundle) {
switch (i) {
case NUMBER_URL:
Log.i("URL_LOADER","LOADING DATA");
// Returns a new CursorLoader
return new CursorLoader(
getActivity(), // Parent activity context
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,// Table to query
null, // Projection to return
null, // No selection clause
null, // No selection arguments
null // Default sort order
);
case EMAIL_URL:
Log.i("URL_LOADER","LOADING DATA");
// Returns a new CursorLoader
return new CursorLoader(
getActivity(), // Parent activity context
ContactsContract.CommonDataKinds.Email.CONTENT_URI, // Table to query
null, // Projection to return
null, // No selection clause
null, // No selection arguments
null // Default sort order
);
default:
Log.i("URL_LOADER","Not Loading");
// An invalid id was passed in
return null;
}
}
@Override
public void onLoadFinished(Loader<android.database.Cursor> loader, android.database.Cursor cursor) {
mAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<android.database.Cursor> loader) {
mAdapter.swapCursor(null);
}
}