使用CursorLoader和Custom CursorAdapter进行ListView过滤

use*_*334 6 android android-listview android-loadermanager android-cursorloader android-cursoradapter

我目前正在做一个项目,涉及根据我当前的位置显示附近的位置列表.

我不久前刚开始使用Android编程,所以在编码阶段我还在学习.

我搜索了所有试图得到一些关于如何继续的线索.阅读和尝试后我仍然被困住了.

我的工作代码目前包括

  • CursorLoader
  • 自定义ResourceCursorAdapter,帮助填充ListView上的条目

问题

  1. 过滤ListView条目的"正确"方法是什么?我在Filter/Filterable界面上看过帖子,但它似乎不适用于我当前的设置?我是否在Custom CursorAdapter中执行过滤?

  2. 执行过滤后,如何刷新ListView?我是否调用getLoaderManager().restartLoader(0,null,this)或adapter.notifyDataSetChanged()?

提前致谢.

kub*_*ubo 13

使用getLoaderManager().restartLoader(LOADER_ID, null, MainActivity.this);召回onCreateLoader.

Android开发者网站示例.

private String filter;
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_filter :
            filter = "COLUMN_NAME = value";
            getLoaderManager().restartLoader(LOADER_ID, null, MainActivity.this);           
            break;          
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public android.content.Loader<Cursor> onCreateLoader(int id, Bundle args) {

        return new CursorLoader(
                MainActivity.this,   // Parent activity context
                SomeContentProvider.CONTENT_URI,        // Table to query
                projection,     // Projection to return
                filter,            // No selection clause
                null,            // No selection arguments
                null             // Default sort order
                );

    }
Run Code Online (Sandbox Code Playgroud)

  • 文字有点稀疏,但这是一个有用的概念。投票赞成。 (2认同)