2

How can I merge cursor loader in the following case to get one Cursor loader. I am calling content provider more than one to create my database adapter to use with simple cursor adapter. Please look my code below.

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {

    productIdCursor.moveToFirst();
    CursorLoader cursorLoader;

    // CursorLoader cursorLoader;

    int i = 0;

    while (productIdCursor.isLast()) {

        String[] Projection = { "p.id as _id", "p.name", "p.subTitle",
                "p.textColour", "pi.thumb" };

        String userSelectionCritera = "p.id = pp.prod_id and pp.imag_id = pi.id and p.id= ? and p.id = pc. prod_id and pc.coun_id = ?";

        String[] selecttionArgs = new String[] {
                productIdCursor.getString(productIdCursor
                        .getColumnIndex("prod_id")),
                String.valueOf(countryId) };

        cursorLoader = new CursorLoader(getActivity(),
                DatabaseContentProvider.CONTENT_URI_PRODUCTCATEGORY,
                Projection, userSelectionCritera, selecttionArgs, null);

        productIdCursor.moveToNext();

    }




    return cursorLoader;
user1154390
  • 2,331
  • 3
  • 25
  • 32

1 Answers1

1

You can't really merge cursor loaders but you can try a few different things. Your best option would probably be to change your SQL selection statement from p.id = ? to p.id in (...id list here...). You can create a string for the id list by having a look at this answer.

You could also create your own custom loader that accepts an array of product ids and loads them all individually in the background, merging them in a MergeCursor object. This would give you a single loader and a cursor with all the data you need. IMO this second approach is really messy. If this is the only working solution then you probably should go back and review your database table schemas.

Community
  • 1
  • 1
ebarrenechea
  • 3,775
  • 1
  • 31
  • 37
  • Thanks for your answer. I knew about first option. Above question clicked in my mind from MergeCursor class. With this class we can merge two cursors. I was curious about same solution possible with loader cursor. – user1154390 Feb 17 '13 at 11:56