解决android - How can I refresh the cursor from a CursorLoader?
2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址:https://cloud.tencent.com/act/cps/redirect?redirect=1062
2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址:https://www.aliyun.com/minisite/goods
推荐:深入源码解析Android中Loader、AsyncTaskLoader、CursorLoader、LoaderManager
如果对Loader、AsyncTaskLoader、CursorLoader、LoaderManager等概念不明白或不知道如何使用Loader机制,可参见博文Android中Loader及LoaderManager的使用(附源
So I have my MainDisplayActivity
which implements both Activity
and LoaderManager.LoaderCallbacks<Cursor>
. Here I have A ListView
, which I fill with Agenda information that I get from my database using a ContentProvider. I also have a GridView
which is a calendar. I have it set up when clicking a cell, that the agenda is updated with the day clicked. My problem is that when reusing the Loader I created in onCreate()
inside of the setOnItemClickListener(), it does not refresh the information with the new cursor I am creating. I can just create a new Loader with another ID, and it works once, but once I click another day, it stops refreshing. The problem, lies in the cursor. How can I refresh a cursor from a Loader so I don't have to keep creating a new Loader? Thanks in advance!
Initial call to create the Loader in onCreate() in My MainDisplayActivity
class:
makeProviderBundle(new String[] {"_id, event_name, start_date, start_time, end_date, end_time, location"},
"date(?) >= start_date and date(?) <= end_date", new String[]{getChosenDate(), getChosenDate()}, null);
getLoaderManager().initLoader(0, myBundle, MainDisplayActivity.this);
list.setAdapter(agendaAdapter);
These are the overridden methods from LoaderCallbacks<Cursor>
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri baseUri = SmartCalProvider.CONTENT_URI;
return new CursorLoader(this, baseUri, args.getStringArray("projection"),
args.getString("selection"), args.getStringArray("selectionArgs"), args.getBoolean("sortOrder") ? args.getString("sortOrder") : null );
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
agendaAdapter.swapCursor(arg1);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
//Not really sure how this works. Maybe I need to fix this here?
agendaAdapter.swapCursor(null);
}
public void makeProviderBundle(String[] projection, String selection, String[] selectionArgs, String sortOrder){
/*this is a convenience method to pass it arguments
* to pass into myBundle which in turn is passed
* into the Cursor loader to query the smartcal.db*/
myBundle = new Bundle();
myBundle.putStringArray("projection", projection);
myBundle.putString("selection", selection);
myBundle.putStringArray("selectionArgs", selectionArgs);
if(sortOrder != null) myBundle.putString("sortOrder", sortOrder);
}
Any additional code needed please do not hesitate to ask. Thanks again for any help!
android debugging android-contentprovider android-loadermanager android-cursorloader|
this question edited May 7 '13 at 20:11 Pi Delport 6,731 2 25 46 asked Jun 18 '12 at 23:43 Andy 4,745 13 59 100 It's been my experience that when you use the
ContentResolver
to update a content in a
ContentProvider
that the
cursor
gets notified and the list gets updated automatically. Could you post your onClick code as well? –
stuckless Jun 19 '12 at 0:17 I know right. I assumed this as well since everyone was saying how convenient that was. But its doesn't do that. The problem is I also understand how that can't work. When the
Loader
is first created, it calls
onCreateLoader()
. When calling
getLoaderManager().initLoader()
, if the first arg, the ID is the same as a
Loader
previously instantiated,
onCreateLoader()
probably won't be called. Of course this is pure speculation on my part as I am still learning, but I figured there had to be a method to load a new Cursor if that was the case. Alex was nice enough to provide it! –
Andy Jun 19 '12 at 1:26
|
1 Answers
1
解决方法
You just need to move your code around a little and call restartLoader
. That is,
When the user clicks a list item, you somehow change the private instance variable that is returned in
getChosenDate()
(I am being intentionally vague here because you didn't specify what exactlygetChosenDate()
returns).推荐:Android Loader(三) 结合CursorLoader分析Loader相关源码
Android Loader(二) CursorLoader Android Loader(四) 自定义Loader从网络中获取文本数据 首先从加载数据的过程开始分析。 初始化Loader的方法是:getLoaderMana
Once the change is made, call
getLoaderManager().restartLoader()
on yourLoader
. Your old data will be discarded andrestartLoader
will triggeronCreateLoader
to be called again. This time around,getChosenDate()
will return a different value (depending on the list item that was clicked) and that should do it. YouronCreateLoader
implementation should look something like this:@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { Uri baseUri = SmartCalProvider.CONTENT_URI; makeProviderBundle( new String[] { "_id, event_name, start_date, start_time, end_date, end_time, location" }, "date(?) >= start_date and date(?) <= end_date", new String[]{ getChosenDate(), getChosenDate() }, null); return new CursorLoader( this, baseUri, args.getStringArray("projection"), args.getString("selection"), args.getStringArray("selectionArgs"), args.getBoolean("sortOrder") ? args.getString("sortOrder") : null); }
Let me know if that makes sense. :)
|
this answer answered Jun 19 '12 at 0:23 Alex Lockwood 66.1k 28 167 220 Holy shiznit lol. Thats genius haha. I see what you are saying. So the method that I could call is
restartLoader()
. How come I don't see that method in CursorLoader?! Oh and sorry,
getChosenDate()
returns a String date in the format used by Databases so I can just do all the filtering with SQLite rather than by java code. –
Andy Jun 19 '12 at 1:04 Dude!!! I love you! It worked beautifully. And looking at my code I realized in
myBundle.putString("date", getChosenDate());
, it was unnecessary. So I took it off. I am just going to use it when calling
makeProviderBundle()
. Thanks a lot... AGAIN! –
Andy Jun 19 '12 at 1:21 10 Haha... you are easily excitable, aren't you. Glad I could help :). –
Alex Lockwood Jun 19 '12 at 1:22 Yes, yes I am. It's just when I've spent the last few days moving my code from deprecated to non deprecated code while learning new features of Android at the same time, feels good to understand it. My first app, so I am pretty hype. Cheers my friend. –
Andy Jun 19 '12 at 1:28 That's what it felt like for me too. Glad it worked out for you! –
Alex Lockwood Jun 19 '12 at 2:44
|
show more comments
原文: http://www.jb51.net/article/37767.htm http://write.blog.csdn.net/posteditref=toolbar android CursorLoader用法介绍 工作内容集中到Contact模块
原文: http://www.jb51.net/article/37767.htm http://write.blog.csdn.net/posteditref=toolbar android CursorLoader用法介绍 工作内容集中到Contact模块
相关阅读排行
- 1Android Https相关完全解析 当OkHttp遇到Https
- 2Android APK反编译详解(附图)
- 3[置顶] Android APK反编译就这么简单 详解(附图)
- 4Android Fragment 真正的完全解析(上)
- 5Android RecyclerView 使用完全解析 体验艺术般的控件