当我恢复此活动时,“尝试重新查询并已关闭游标”.在我开始使用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.因此,您必须在那里检查查询字符串以设置您的选择.
@OverrIDepublic 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); }总结
以上是内存溢出为你收集整理的Android CursorLoader全部内容,希望文章能够帮你解决Android CursorLoader所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)