FilterQueryProvider,过滤器和ListView [英] FilterQueryProvider, filter and ListView

查看:105
本文介绍了FilterQueryProvider,过滤器和ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,如下所示:

I have a database as follows:

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

我显示这个数据库用户 通过利用CustomSimpleCursorAdapter的。

I am showing this database to the user by making use of a CustomSimpleCursorAdapter.

CustomSimpleCursorAdapter extends SimpleCursorAdapter

实现的可筛选

implements Filterable

getView()&放大器; runQueryonBackgroundThread()被覆盖。
书籍的网格视图中正常显示。

with getView() & runQueryonBackgroundThread() being overriden.
The Grid view of books is properly shown.

用户有以下选项:

HTML | PDF | PS |删除

HTML | PDF | PS | DELETE

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

当用户presses HTML菜单选项,使用HTML书籍 型已被示出。

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

MenuOption处理器()里面,我写如下:

inside MenuOption handler(), I wrote as follows:

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

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

这约束达到我重写 runQueryonBackgroundThread() 方法。但它没有更新网格视图并抛出异常。

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

FILTER:android.view.ViewRoot $ CalledFromWrongThreadException:只有   创建一个视图层次可以触摸的意见原帖

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

请帮我。

推荐答案

我想你已经搞砸了事情有点。其实 SimpleCursorAdapter 已经实现了可筛选,所以没有必要重新实现它。相反,在你的 ListActivity 使用水木清华这样的:

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

这篇关于FilterQueryProvider,过滤器和ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆