ListView filtering with CursorLoader AND FilterQueryProvider?

我与影子孤独终老i 提交于 2019-12-03 16:44:40

A little bit late but I have faced exact same problem and comments were not well explanatory. For those who faces the problem here is the solution:

"Stop using FilterQueryProvider. CursorLoader is also a replacement for that: restart it with your filter and you'll be fine." (source)

Which means;

instead of doing this:

@Override
public Cursor runQuery(CharSequence listId) {
    return mDb.getContacts(listId);  
}// DO NOT set direct database query, has no observation at all.

or

@Override
public Cursor runQuery(CharSequence listId) {
    return getContentManager.query(....,new String[]{listId}...
}// DO NOT return content manager result, forgets observers.

or

@Override
public Cursor runQuery(CharSequence listId) {
    Bundle bundle = new Bundle();
    bundle.putCharSequence("selected", listId);
    getLoaderManager().restartLoader(CURSOR_LOADER_CONTACTS, bundle, MainActivity.this);
    return null;
}// DO NOT restart the cursor loader with new info, screws observers. 

with combination of mContactAdapter.getFilter().filter(id+""); (while applying filter)

do this:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    switch (id) {
        case CURSOR_LOADER_CONTACTS:
            long mListId = 0;
            if (args != null) {
                mListId = args.getLong(CONTACT_FILTER);
            }
            return new CursorLoader(this, ContentProvider.CONTACT_DISPLAY_URI, null, ContactTable.COLUMN_GROUP_ID+"=?", new String[]{id+""}, null); 
    } //Apply filter here with new info comes from 'restartLoader'
    return null;
}

with combination of getLoaderManager().restartLoader(MainActivity.CURSOR_LOADER_CONTACTS, bundle, MainActivity.this);

assuming MainActivity.this implements LoaderManager.LoaderCallbacks<Cursor>

when using FilterQueryProvider use restartLoader instead of initLoader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!