如何异步运行 FilterQueryProvider 的查询?

发布于 2024-12-08 20:05:35 字数 223 浏览 0 评论 0原文

我使用 FilterQueryProvider 来过滤由自定义 CursorAdapter 支持的列表视图的内容。

要使用 FilterQueryProvider,您必须重写返回 Cursor 对象的 runQuery() 方法。现在我想知道如何异步查询光标以避免阻塞 UI 线程。

有某种最佳实践吗?我找不到任何有关 runQuery() 方法是在 UI 线程上执行还是在其自己的线程上执行的信息。

I'm using a FilterQueryProvider to filter the content of a list view which is backed up by a custom CursorAdapter.

To use the FilterQueryProvider you have to override the runQuery() method which returns a Cursor object. Now I'm wondering how to query for the cursor asynchronously to avoid blocking the UI thread.

Is there some kind of best practice? I couldn't find any information whether the the runQuery() method is executed on the UI-thread or on its own thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

壹場煙雨 2024-12-15 20:05:35

从文档中:

通过调用filter(CharSequence,
android.widget.Filter.FilterListener) 异步执行

,因此您的代码应如下所示:

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 
            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) {
        return dbHelper.getListCursor(constraint);
    }
};

来源:这个

From the documentation :

Filtering operations performed by calling filter(CharSequence,
android.widget.Filter.FilterListener) are performed asynchronously

So your code should look 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 
            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) {
        return dbHelper.getListCursor(constraint);
    }
};

Sources : this and this

凉月流沐 2024-12-15 20:05:35

根据 CursorAdapter 文档可以使用CursorAdapter#runQueryOnBackgroundThread

According to CursorAdapter documentation you can use CursorAdapter#runQueryOnBackgroundThread

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文