Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

android - 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 original thread that created a view hierarchy can touch its views"

Please help me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...