How can I refresh the cursor from a CursorLoader?
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).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. :)
Related videos on Youtube
Andy
Love programming. Focused mainly on Android and Ruby on Rails full stack development. I also dabbles in the PHPs. SOreadytohelp
Updated on May 08, 2020Comments
-
Andy almost 4 years
So I have my
MainDisplayActivity
which implements bothActivity
andLoaderManager.LoaderCallbacks<Cursor>
. Here I have AListView
, which I fill with Agenda information that I get from my database using a ContentProvider. I also have aGridView
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 inonCreate()
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!
-
stuckless almost 12 yearsIt's been my experience that when you use the
ContentResolver
to update a content in aContentProvider
that thecursor
gets notified and the list gets updated automatically. Could you post your onClick code as well? -
Andy almost 12 yearsI 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 callsonCreateLoader()
. When callinggetLoaderManager().initLoader()
, if the first arg, the ID is the same as aLoader
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 almost 12 yearsHoly 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 almost 12 yearsDude!!! 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 callingmakeProviderBundle()
. Thanks a lot... AGAIN! -
Alex Lockwood almost 12 yearsHaha... you are easily excitable, aren't you. Glad I could help :).
-
Andy almost 12 yearsYes, 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.
-
Alex Lockwood almost 12 yearsThat's what it felt like for me too. Glad it worked out for you!
-
Alex Lockwood almost 12 years@Andy, did you end up figuring this out?
-
Andy almost 12 yearsI have a question on my code here. My String arrays, when sending them into
makeProviderBundle()
, it works, but its a String array, but technically its a single string. Is it suppose to be each column its own individual String? It also leads me to wonder why it worked. It says that if null, all columns included, but if its not null, but a single string, then how did it still compile correctly?