how to use join query in CursorLoader when its constructor does not support it

I am having two tables with 1:n relationship, I am using content provider and cursorloader.

How would I make a join query to work with cursor loader? I could hack it up somehow with rawSql inside content provider but how to do it in cursor loader constructor is beyond me.

  • Linker errors in Android NDK (undefined reference to `__cxa_end_cleanup')
  • Google Play Game Services sign in button spits “Unknown error”
  • Unicode character on Android not displaying
  • How do I set multiple input types in an EditText on Android?
  • Android toolbar setNavigationIcon not working
  • Showing Thumbnail for link in WhatsApp || og:image meta-tag doesn't work
  • Thanks a lot !

    CursorLoader(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

    How will query for join when Uri could only point to one table

    Related posts:

    Android - Facebook share content gets overwritten
    Android Account Authenticator edit Email Id Credentials
    Query MediaStore Cursor using specific folder
    Invalid Gradle JDK configuration found?
    Google Play Services with AdMob. Check availability?
    How to use coordinator layout with fragment as “scrolling view”
  • Android Studio “can't resolove symbol 'Theme'”
  • Noise Algorithm fails in Samsung Galaxy SIII (GLES)
  • Android Could not find android-support-v7-appcompat.apk error
  • Android emulator with proxy settings - Error FATAL:.//android/base/sockets/ Check failed: isValidFd(fd). fd 1404 max1024
  • changing CharSequence first letter to upper case in android
  • Error in maven pom xml file: Building Android project
  • 2 Solutions collect form web for “how to use join query in CursorLoader when its constructor does not support it”

    The Uri does not point to any table. It points to whatever you feel like pointing it to.

    Let’s pretend that your two tables are Customer and Order. One customer may have many orders. You want to execute a query to get all outstanding orders… but you want to join in some customer-related columns that you will need, such as the customer’s name.

    Let’s further pretend that you already have content:// and content:// defined to purely query those tables.

    You have two choices:

    1. Add the join of the customer’s display name on your /order Uri. Having another available column probably will not break any existing consumers of the provider (though testing is always a good idea). This is what ContactsContract does — it joins in some base columns, like the contact’s name, on pretty much all queries of all tables.

    2. Create content:// that does the same basic query as /order does, but contains your join. In this case, you could have insert(), update(), and delete() throw some sort of RuntimeException, to remind you that you should not be modifying data using /orderWithCust as a Uri.

    In the end, designing a ContentProvider Uri system is similar to designing a REST Web service’s URL system. In both cases, the join has to be done on the provider/server side, and so you may need to break the one-table-to-one-URL baseline to offer up some useful joins.

    I found a solution using sub-classes of ContentProvider.
    Let’s say you have table tblA and another table tblB. I recommend creating two classes “AContentProvider” and “BContentProvider”. Most importantly, make sure that both tables are set up in the same database.

    The main part of the solution is to override ContentProvider.query() in the ContentProvider that you will call from your CursorLoader – the URI decides which one it is:

    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sort) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
                "tblA LEFT JOIN tblB"
                        + " ON ("
                        + "tblA.b_id"
                        + " = "
                        + ""
                        + ")"
        // Content of projection is set by CursorLoader
        // usually in an Activity that implements LoaderManager.LoaderCallbacks<>
        Cursor c = qb.query(
        return c;

    As you can see, the JOIN is done in setTables(). Using projection you make sure that you only display the columns you really need and most of all, you have no duplicate columns, like “id” from both tables:

    final String[] projection = new String[] {

    Make use of the override and try to get as much work done in the sub-classes as you can; for example: all my overridden query() methods call setNotificationUri() to notify the ContentResolver if the cursor result set changes.:

    c.setNotificationUri(getContext().getContentResolver(), uri);
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.