Android CursorLoader

前端之家收集整理的这篇文章主要介绍了Android CursorLoader前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚开始使用新的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);
    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,_id from codes where parent_id match " + parent_id + "";

    //startManagingCursor(cCodes);
    Cursor[] c = {cTitles,cCodes};
    Cursor cursor = new MergeCursor(c);
    startManagingCursor(cursor);

    return cursor;

}

解决方法

我不相信从loader.loadInBackground()捕获Cursor就是你想要的.
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,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);
    }
原文链接:https://www.f2er.com/android/313957.html

猜你在找的Android相关文章