开发者

CursorLoader with rawQuery

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 wou开发者_运维知识库ld appreciate it. Otherwise I will have to probably create my own CursorLoader class with the necessary functionality, which I'm trying to avoid.


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.


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);
    }
[...]
}


**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;


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜