开发者

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 am showing this database to the user by making use of a CustomSimpleCursorAdapter.

CustomSimpleCursorAdapter extends SimpleCursorAdapter

implements Filterable

with getView() & runQueryonBackgroundThread() being overriden.

The Grid view of books is properly shown.

The user has the following options:

HTML | PDF | PS | DELETE

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

When the user presses HTML menu option, the books with HTML type has to be shown.

inside MenuOption handler(), I wrote as follows:

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

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

This constraint reaching my overriden runQueryonBackgroundThread() method. But its not updating the grid view and throws an exception.

"FILTER: android.view.ViewRoot$CalledFromWrongThreadException: Only the 开发者_Python百科 original thread that created a view hierarchy can touch its views"

Please help me.


I think you've messed up things a bit. Actually SimpleCursorAdapter already implements Filterable, so there's no need to reimplement it. Instead in your ListActivity use smth like this:

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);
    }
};
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜