【问题标题】:FilterQueryProvider, filter and ListViewFilterQueryProvider、过滤器和ListView
【发布时间】:2011-02-20 13:42:22
【问题描述】:

我有一个数据库如下:

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

我正在向用户展示这个数据库 通过使用 CustomSimpleCursorAdapter。

CustomSimpleCursorAdapter extends SimpleCursorAdapter

实现可过滤

getView() & runQueryonBackgroundThread() 被覆盖。
书籍的网格视图已正确显示。

用户有以下选择:

HTML | PDF格式 |附言 |删除

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

当用户按下 HTML 菜单选项时,带有 HTML 的书籍 必须显示类型。

在MenuOption handler()里面,我是这样写的:

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

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

这个约束到达我的覆盖runQueryonBackgroundThread() 方法。但它没有更新网格视图并引发异常。

"FILTER: android.view.ViewRoot$CalledFromWrongThreadException: 只有 创建视图层次结构的原始线程可以触及其视图"

请帮帮我。

【问题讨论】:

    标签: android


    【解决方案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);
        }
    };
    

    【讨论】:

    • 嗨,Arhimed,非常感谢。它工作得很好。 :) 我也会尝试如何在 CustomSimpleCursorAdapter 的 runQueryonBT 中做到这一点。
    • 这很有趣。使用装载机时这仍然有效吗?我的意思是使用过滤器查询提供程序比简单地执行 restartLoader() 有什么优势吗?看我的问题stackoverflow.com/questions/24344950/…
    • @faizal:我没有深入研究过loader,所以无权向你推荐任何东西。
    猜你喜欢
    • 1970-01-01
    • 2012-09-27
    • 2019-08-14
    • 2013-03-17
    • 2023-02-01
    • 2016-05-09
    • 1970-01-01
    • 2018-09-24
    • 2011-10-13
    相关资源
    最近更新 更多