问题描述
我使用的是FilterQueryProvider到由一个自定义的CursorAdapter备份列表视图的内容进行过滤。
I'm using a FilterQueryProvider to filter the content of a list view which is backed up by a custom CursorAdapter.
要使用FilterQueryProvider你必须重写它返回一个游标对象runQuery()方法。现在,我不知道如何为游标查询以异步方式,以避免阻塞UI线程。
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.
是否有某种最佳实践?我找不到无论是在UI线程或它自己的线程执行的runQuery()方法的任何信息。
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.
推荐答案
从文档:
过滤操作,
android.widget.Filter.FilterListener)都是异步执行
所以你的code应该是这样的:
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);
}
};
来源:和这个
这篇关于如何异步运行FilterQueryProvider的查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!