7

I'm looking into implementing CursorLoader in my app but I'm having a small issue that it seems that there isn't a way to just a pass a raw query to the CursorLoader constructor.

I maybe missing something in the documentation (and google), so if anybody can point me to a simple way to run a raw query with a CursorLoader class I would appreciate it. Otherwise I will have to probably create my own CursorLoader class with the necessary functionality, which I'm trying to avoid.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66

4 Answers4

8

it seems that there isn't a way to just a pass a raw query to the CursorLoader constructor.

That is because CursorLoader works with content providers, and content providers do not support rawQuery().

so if anybody can point me to a simple way to run a raw query with a CursorLoader class I would appreciate it.

That is impossible, sorry. You are welcome to create your own AsyncTaskLoader that hits a SQLite database and supports rawQuery(). In fact, I will probably write one of these later this year, if I don't see where anyone has beaten me to it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • This would be a start: http://stackoverflow.com/questions/7182485/usage-cursorloader-without-contentprovider – Bryan Denny Apr 28 '12 at 19:00
  • I'm not sure this is completely true, and may have caused me 2 days of red herrings. I have a rawQuery in my SQLiteOpenHelper where I have all my actual queries. I call that from a content provider started by a cursorloader and it seems to work fine. – kpierce8 Jun 23 '12 at 23:15
  • @kpierce8: The OP was trying to pass a "raw query" to `CursorLoader`, which is not strictly supported. The OP did not discuss the *implementation* of a `ContentProvider`, which is where you are using `SQLiteOpenHelper` and `rawQuery()`. Now, it is certainly possible for somebody to implement a `ContentProvider` where a "raw query" was passed in, say, the 3rd parameter to `query()`, instead of a simple `WHERE` clause (or equivalent). And, for that specific `ContentProvider`, you could pass a "raw query" to `CursorLoader`. However, that is not a typical approach. – CommonsWare Jun 23 '12 at 23:20
7

Raw query is not supported directly, but you can do a dirty hack: from your code call getContentResolver().query(RAWQUERY_CONTENT_URI, null, rawquery, args, null); and implement content provider like

@Override
public synchronized Cursor query(Uri uri, String[] projection, String selection,
       String[] selectionArgs, String sortOrder)
{
    int uriType = sURIMatcher.match(uri);
    switch (uriType)
    {
        case RAW_QUERY:
        return dbHelper.getReadableDatabase().rawQuery(selection, selectionArgs);
    }
[...]
}
tomash
  • 12,742
  • 15
  • 64
  • 81
1

**For Custom Search using Content provider **

Change Cursor Loader as Follow (in onCreateLoader )

return new CursorLoader(
        getActivity(),                        // Context
        PRODUCT.CONTENT_URI,                  // URI
        PROJECTION,                           // Projection
        PRODUCT.PRODUCT_NAME+ " like ?",      // Selection
        new String[]{"%" + mCurFilter + "%"}, // Selection args
        PRODUCT.PRODUCT_NAME + " asc"); 

In your Provider Change Accordingly

//C is Cursor object
switch (uriMatch) {
        case ROUTE_PRODUCT_ID:
            // Return a single entry, by ID.
            String id = uri.getLastPathSegment();
            builder.where(PRODUCT._ID + "=?", id);
            c = builder.query(db, projection, sortOrder);
            assert ctx != null;
            c.setNotificationUri(ctx.getContentResolver(), uri);
            return c;
//          break;
        case ROUTE_PRODUCT:
            // Return all known entries.
            builder.table(PRODUCT.PRODUCT_TABLE_NAME)
            .where(selection, selectionArgs);
            c = builder.query(db, projection, sortOrder);
            assert ctx != null;
            c.setNotificationUri(ctx.getContentResolver(), uri);
            return c;
asontu
  • 4,548
  • 1
  • 21
  • 29
0

You can implement your own CursorLoader with raw query. This is the source of the original CursorLoader: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/content/CursorLoader.java

Faz
  • 192
  • 2
  • 6