0

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);
    }
}
Rakesh Rav
  • 47
  • 1
  • 9
  • you cannot get all the emails and phone numbers using one `Cursor`, **ok** you can do that but the resulting `Cursor` would be useless for any `[Simple]CursorAdapter` – pskink Sep 30 '15 at 06:54
  • @pskink so is there any way that i can get these two values, using cursor loader. – Rakesh Rav Sep 30 '15 at 07:30
  • yes but you will receive two `Cursors`, is it what you want? – pskink Sep 30 '15 at 07:35
  • @pskink what i want, is to return a cursor loader with 2 column data containing number and email address is it possible. – Rakesh Rav Sep 30 '15 at 07:44
  • or can you tell me that how to perform join in content uri's so that i can join these two content uri's and get the 2 column result in the single cursor loader and return it – Rakesh Rav Sep 30 '15 at 07:46
  • you cannot do that, all you can do is use [this](http://stackoverflow.com/a/26820544/2252830) or use a `CursorJoiner` – pskink Sep 30 '15 at 07:52
  • or you can use something like [this](https://gist.github.com/pskink/6564e6a66b95e228db7b) , but it is a more complex solution as it creates a wrapped `Cursor`... – pskink Sep 30 '15 at 08:31
  • thank u i will look into it..... – Rakesh Rav Sep 30 '15 at 09:15

0 Answers0