Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change query in a Loader<Cursor> after notifyChange

I have tried for hours but couldn't achieve this. I tried sending via Bundle, via static variables, via if statement but nothing worked.

I have a situation where I want my Loader to load different sets of data once user has clicked a menu item. This means the query should be changed once it has received the notifyChange. The code is pretty standard Loader code like this:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String query = args.getString("query");

    String[] projection = { DbCreate.ID, DbCreate.COL_ITEM, DbCreate.COL_STATUS, DbCreate.COL_SYNCED,
            DbCreate.COL_PRIORITY, DbCreate.COL_COMPLETEBY, DbCreate.COL_TAG, DbCreate.COL_ASSIGNED, DbCreate.COL_SHARED};

    CursorLoader cursorLoader = new CursorLoader(getActivity(),
            DbProvider.CONTENT_URI_DATA, projection, query, null, "priority DESC, status ASC, _id DESC");

    return cursorLoader;
}

I tried usual if(...) statement inside this onCreate method too but it doesn't work. This means the notifyChange just triggers the already created object. So how can I inject a new 'query' value on the notifyChange?

like image 738
zeeshan Avatar asked Jan 19 '26 11:01

zeeshan


1 Answers

I am doing same thing in my code you need to restart cursor loader again when you want to change the data notify works on adapter not on cursor loader

@Override
    public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
        // This is called when a new Loader needs to be created. 
        // Now create and return a CursorLoader that will take care of
        // creating a Cursor for the data being displayed.
        Uri contentUri = MyContentProvider.CONTENT_URI_FACEBOOK;
        switch (id) {
        case FACEBOOK:
            contentUri = MyContentProvider.CONTENT_URI_FACEBOOK;
            break;
        case LINKEDIN:
            contentUri = MyContentProvider.CONTENT_URI_LINKEDIN;
            break;
        }

        return new CursorLoader(getActivity(), contentUri, null, null, null,
                null);

    }

for various queries I am doing like this only

getLoaderManager().restartLoader(FACEBOOK, null, Profile.this);
getLoaderManager().restartLoader(LINKEDIN, null, Profile.this);

so you just need to restart loader

like image 67
Akhil Dad Avatar answered Jan 22 '26 01:01

Akhil Dad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!