【问题标题】:Android CursorLoader, Trying to requery an already closed cursorAndroid CursorLoader,试图重新查询已经关闭的游标
【发布时间】:2011-08-28 13:03:10
【问题描述】:

我刚开始使用新的 cursorLoader,但遇到了问题。以下代码只是为了了解 cursorLoader 的工作原理,但我不断得到:

“尝试重新查询已关闭的游标”,当我恢复此活动时。在我开始使用 cursorLoader 之前,该应用程序运行良好。有什么想法吗?

private Cursor getSectionData(CharSequence parent_id) {
    
    String[] projection = new String[] {Titles.SECTION, Titles.TITLE, Titles._ID, Titles.CODE_RANGE,};
    Uri titles =  Titles.CONTENT_URI;
    String select = "" + Titles.PARENT_ID + " match " + parent_id + "";
    CursorLoader loader = new CursorLoader(this, titles, projection, select, null, null);
    Cursor cTitles = loader.loadInBackground();
    
    
    String[] projection1 = new String[] {Codes.CODE, Codes.EXCERPT, Codes._ID,};
    Uri codes =  Codes.CONTENT_URI;
    String select1 = "" + Codes.PARENT_ID + " match " + parent_id + "";
    CursorLoader loader1 = new CursorLoader(this, codes, projection1, select1, null, null);
    Cursor cCodes = loader1.loadInBackground();

    
    
    //Cursor cTitles = db.rawQuery("select section, title, _id, code_range from titles where parent_id match " + parent_id + "", null);
    //startManagingCursor(cTitles);
    //Cursor cCodes = db.rawQuery("select code, excerpt, _id from codes where parent_id match " + parent_id + "", null);
    
    mQuery = "select code, excerpt, _id from codes where parent_id match " + parent_id + "";
    
    //startManagingCursor(cCodes);
    Cursor[] c = {cTitles, cCodes};
    Cursor cursor = new MergeCursor(c);
    startManagingCursor(cursor);
    
    return cursor;

}

【问题讨论】:

    标签: android android-cursorloader


    【解决方案1】:

    我不相信从 loader.loadInBackground() 捕获光标是你想要的。 loadInBackground() 的实现基本上是执行查询并返回 UI 线程上的光标,这是您要避免的。

    当您在 UI 线程上等待返回值时,请务必小心。这应该是一个很好的指标,表明这不是你想要的。

    我解决此问题的方法是重新启动加载程序。 就我而言,我正在尝试使用操作栏来搜索我的内容。 我的班级扩展了 ListFragment 并实现了 LoaderManager.LoaderCallbacks 容纳它的 Activity 实现了 OnQueryTextListener 并在这里调用片段是我在片段中执行的代码。

    public void doSearch(String query) {
        Bundle bundle = new Bundle();
        bundle.putString("query", query);
    
        getLoaderManager().restartLoader(LoaderMangerIdHelper.INVENTORY, bundle, this);
    }
    

    请注意,您必须重新启动加载程序。装载机全部由系统管理。这将导致您的 onCreateLoader 被重新调用。所以你必须检查那里的查询字符串来设置你的选择。

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
        String SELECTION = "someColumn=?";
        List<String> selectionArgs = new ArrayList<String>();
        selectionArgs.add("someArgs");
    
        if (bundle != null && bundle.containsKey("query")) {
            SELECTION += " AND columnTitle LIKE ?";
            selectionArgs.add(bundle.getString("query") + "%");
        }
    
        final String[] SELECTION_ARGS = new String[selectionArgs.size()];
        selectionArgs.toArray(SELECTION_ARGS);
    
        mLoader = new CursorLoader(getActivity(), RoamPay.Product.CONTENT_URI, null, SELECTION,
                SELECTION_ARGS, null);
    
        return mLoader;
    }
    

    这会在后台启动 cursorLoading。并且回调应该和往常一样。

       @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
            // Swap the new cursor in. (The framework will take care of closing the
            // old cursor once we return.)
            mAdapter.swapCursor(data);
    
            // The list should now be shown.
            if (isResumed()) {
                setListShown(true);
            } else {
                setListShownNoAnimation(true);
            }
        }
    
        @Override
        public void onLoaderReset(Loader<Cursor> loader) {
            // This is called when the last Cursor provided to onLoadFinished()
            // above is about to be closed. We need to make sure we are no
            // longer using it.
            mAdapter.swapCursor(null);
        }
    

    【讨论】:

    • 我得到 Loader 类型中的方法 registerListener(int, Loader.OnLoadCompleteListener) 不适用于参数 (int, LoaderManager.LoaderCallbacks)
    • 这篇文章有点老了,不确定他们是否更新了一些东西。您是否尝试过创建 OnLoadCompleteListener 而不是使用 LoaderCallbacks?
    猜你喜欢
    • 1970-01-01
    • 2011-08-20
    • 2013-04-18
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2015-03-06
    • 1970-01-01
    相关资源
    最近更新 更多