FilterQueryProvider, filter and ListView

I have a database as follows:

------------------------------
BOOK NAME | BOOK FORMAT | COUNT |
------------------------------
Android   | HTML       | 1
WPF       | PDF        | 10
Symbian   | PS         | 2
Windows   | HTML       | 2

I show this database to the user using CustomSimpleCursorAdapter.

CustomSimpleCursorAdapter extends SimpleCursorAdapter

implements filterable

when getView()u runQueryonBackgroundThread()will be redefined.
The view of the grid table has been removed.

The user has the following options:

HTML | Pdf | PS | REMOVE

Constraint: BOOK FORMAT
[HTML - 1, PDF - 2, PS - 3] 

When the user clicks the HTML button, books with the HTML type should be displayed.

inside the MenuOption () handler, I wrote the following:

adapter.getFilter().filter("1");

runQueryonBackgroundThread() {
    if(mCursor != null)
        mCursor.close();
    mCursor = query(using the constraint)
    return mCursor;
}

This limit reaches my excess runQueryonBackgroundThread() method. But it does not update the grid view and throws an exception.

"FILTER: android.view.ViewRoot $ CalledFromWrongThreadException: only the original thread that created the view hierarchy can touch its views"

Please help me.

+3
1

, . SimpleCursorAdapter Filterable, . ListActivity smth :

private void filterList(CharSequence constraint) {
    final YourListCursorAdapter adapter = 
        (YourListCursorAdapter) getListAdapter();
    final Cursor oldCursor = adapter.getCursor();
    adapter.setFilterQueryProvider(filterQueryProvider);
    adapter.getFilter().filter(constraint, new FilterListener() {
        public void onFilterComplete(int count) {
            // assuming your activity manages the Cursor 
            // (which is a recommended way)
            stopManagingCursor(oldCursor);
            final Cursor newCursor = adapter.getCursor();
            startManagingCursor(newCursor);
            // safely close the oldCursor
            if (oldCursor != null && !oldCursor.isClosed()) {
                oldCursor.close();
            }
        }
    });
}

private FilterQueryProvider filterQueryProvider = new FilterQueryProvider() {
    public Cursor runQuery(CharSequence constraint) {
        // assuming you have your custom DBHelper instance 
        // ready to execute the DB request
        return dbHelper.getListCursor(constraint);
    }
};
+8

Source: https://habr.com/ru/post/1793387/


All Articles