How can I refresh the cursor from a CursorLoader? - android

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!

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 exactly getChosenDate() returns).
Once the change is made, call getLoaderManager().restartLoader() on your Loader. Your old data will be discarded and restartLoader will trigger onCreateLoader 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. Your onCreateLoader 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

how to preventing the cursorLoader calling into the onLoadFinished if the data it is monitoring changes

Having fragment like below, in normal flow the onCreateLoader and onLoadFinished is called in pair.
But when the datasource (the database) content is changed, and since the loader is monitoring the data change the loader will issue another call to onLoadFinished() with new data filled in cursor.
But In my case it does not want to change the current cursor in use, so don't want the loader deliver the updated cursor vis another onLoadFinished call, or disable the loader's monitoring part.
Is there a way to do it?
AFragment extends Fragment implements LoaderCallbacks<Cursor> {
protected void startSupportLoaderManager() {
getActivity().getSupportLoaderManager()
.initLoader(LOADER_ID, null, this);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return createLoader(getActivity(), id, args, null);
}
#Override
public void onLoaderReset(Loader<Cursor> arg0) {
if (mAdapter != null) {
mAdapter.resetCursor();
}
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
...
}
}
EDIT:
Kinda of knowing this may work, but feel still missing some dot. Here is what the thought:
In the implementation of ContentProvider, for insert(), update(), delete() we do
getContext().getContentResolver().notifyChange(uri, null);
And in the CursorLoader it did cursor.registerContentObserver(mObserver); in
public Cursor loadInBackground() {
Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection,
mSelectionArgs, mSortOrder);
if (cursor != null) {
// Ensure the cursor window is filled
cursor.getCount();
cursor.registerContentObserver(mObserver);
}
return cursor;
}
So it in some way is monitoring the Uri data source change, same as we do on cursor
cursor.setNotificationUri(getContext().getContentResolver(), uri);
So if we could provide a Uri, which could used for insert/update/delete, but is different than the Uri we give to the Loader, that would still do the inert/update/delet operation but the loader will not be notified because of the Uri is different.
So basically, the loader will use different Uri than the Uri the other data operation using.
Not sure If understanding how the Loader's monitoring with the content Uri is correct. Maybe there is better way of doing it?
Any suggestion is appreciated.
If you want to stop the loader, you can use:
getLoaderManager().destroyLoader(LOADER_ID);

is there a way of modifying values loaded via CursorLoader and just before displaying to the textview

I'm using plain old ListViews, SimpleCursorAdapter, LoaderCallback etc. to read values from a database and display in textViews.
sample code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cateory_list);
mListView = (ListView) findViewById(R.id.list_view);
mAdapter = new SimpleCursorAdapter(
this,
R.layout.category_parent,
null,
new String[] {CategoryTable.COL_2},
new int[] {R.id.text_view},
0);
mListView.setAdapter(mAdapter);
getLoaderManager().initLoader(0, null, this);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Uri uri = new Uri.Builder()
.scheme("content")
.appendPath(CategoryTable.TB_NAME)
.authority("com.example.auth")
.build();
return new CursorLoader(this, uri, null, null, null, null);
}
Everything works well and the values from database are displayed on the listview. But suppose I want to do some text processing of the values before displaying, how can I do that?
Edit 1: I don't want to do the text processing in main-thread. Is there a way I can use the AsynTaskLoader thread created from CursorLoader and off-load the work over there?
Yep. It is called a ViewBinder. You want a SimpleCursorAdapter.ViewBinder, in your case. It is called just as the data is moved from the adapter, in to the view.
Be careful, though. It is called a lot (likely several times for each view in each cell of the list). It needs to run really fast and, unless you want to drive the GC nuts, should not allocate anything.
I managed to create a Custom Loader by extending CursorLoader and only overriding public Cursor loadInBackground()
This way I'm retrieving the data, do long running text processing, insert the result back to new table and return the cursor of the new table.
Sample code:
#Override
public Cursor loadInBackground() {
Cursor cursor = super.loadInBackground();
cursor.moveToFirst();
try {
ActiveAndroid.beginTransaction();
do {
String s = doProcessing(cursor.getString(colNumber));
createAndInsertIntoNewTable(s);
} while (cursor.moveToNext());
ActiveAndroid.setTransactionSuccessful();
} finally {
ActiveAndroid.endTransaction();
}
return cursorFromNewTable;
}
This completely does the work in AsyncTaskLoader thread and solves my problem perfectly.

How to change query in a Loader<Cursor> after notifyChange

I have tried for hours but couldn't achieve this. I tried sending via Bundle, via static variables, via if statement but nothing worked.
I have a situation where I want my Loader to load different sets of data once user has clicked a menu item. This means the query should be changed once it has received the notifyChange. The code is pretty standard Loader code like this:
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String query = args.getString("query");
String[] projection = { DbCreate.ID, DbCreate.COL_ITEM, DbCreate.COL_STATUS, DbCreate.COL_SYNCED,
DbCreate.COL_PRIORITY, DbCreate.COL_COMPLETEBY, DbCreate.COL_TAG, DbCreate.COL_ASSIGNED, DbCreate.COL_SHARED};
CursorLoader cursorLoader = new CursorLoader(getActivity(),
DbProvider.CONTENT_URI_DATA, projection, query, null, "priority DESC, status ASC, _id DESC");
return cursorLoader;
}
I tried usual if(...) statement inside this onCreate method too but it doesn't work. This means the notifyChange just triggers the already created object. So how can I inject a new 'query' value on the notifyChange?
I am doing same thing in my code you need to restart cursor loader again when you want to change the data notify works on adapter not on cursor loader
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
// This is called when a new Loader needs to be created.
// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
Uri contentUri = MyContentProvider.CONTENT_URI_FACEBOOK;
switch (id) {
case FACEBOOK:
contentUri = MyContentProvider.CONTENT_URI_FACEBOOK;
break;
case LINKEDIN:
contentUri = MyContentProvider.CONTENT_URI_LINKEDIN;
break;
}
return new CursorLoader(getActivity(), contentUri, null, null, null,
null);
}
for various queries I am doing like this only
getLoaderManager().restartLoader(FACEBOOK, null, Profile.this);
getLoaderManager().restartLoader(LINKEDIN, null, Profile.this);
so you just need to restart loader
If I understand your problem correctly, you probably don't want to worry about trying to change the dataset via notifyChange. I think you most likely want to simply fire-off a new loader (or do a forced reload on the existing one). Let the loader set the adapter on load completion and that will cause a notify on the data set.
I.E. User clicks menu, and you fire the loader with the new query parameters/other info. If the loader is already running, you can cancel it and start a new one etc.

Query with CursorLoader in ListFragment isn't returning any data

I continue to struggle with getting a query to work with a CursorLoader in a ListFragment. I suspect part of my problem is that I'm unsure about certain details. I have an xml file, myfragment.xml, which defines the two fragments in my app. The first fragment, my ListFragment, is identified by:
android:id="#+id/frag_mylist"
When I call SimpleCursorAdapter in my ListFragment class, I believe I should do this:
String[] dataColumns = { "fieldname", "_id" };
int[] viewIDs = { R.id.frag_mylist };
mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.myfragment, null, dataColumns, viewIDs, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, info, (LoaderCallbacks<Cursor>) this);
where info is a Bundle that I've passed from a previous activity. Is that right? Also, I've seen some examples with 0 as the last parameter for SimpleCursorAdapter, others with CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER. What's the difference? Finally, this page may indicate that I have to retrieve a LoaderManager in my code like so:
private LoaderManager mLoaderManager;
public void onCreate(savedInstanceState) {
super.onCreate(savedInstanceState);
mLoaderManager = this.getSupportLoaderManager();
}
but this is the only place I've seen this. Is this necessary? I'm hoping that getting answers to these questions will help me dig down to why my query is returning no results. I'm fairly confident that my database is being created and populated at this point. Thanks much!
As requested below, here are the three methods of my LoaderManager.LoaderCallbacks interface:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selection = "level='" + args.getString("Level") + "'";
return (Loader<Cursor>) new CursorLoader(getActivity(), MY_URI,
PROJECTION, selection, null, null);
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
switch (loader.getId()) {
case LOADER_ID:
mAdapter.swapCursor((android.database.Cursor) cursor);
break;
}
}
public void onLoaderReset(Loader<Cursor> loader) {
mAdapter.swapCursor(null);
}
Let me add that I've verified through the debugger that args.GetString("Level") in the onCreateLoader method is "Beginning", which is what it should be.
Add this line within your onLoadFinished after you swap the cursor
mAdapter.notifyDataSetChanged()

Why cursorLoader didn't notify changes in source data?

I have a simple contentProvider, a layout with a ListView and a button for adding Items in content Provider and a CursorLoader. The android.content.Loader, D reference states that
The Loader will monitor for changes to the data, and report them to
you through new calls here. You should not monitor the data yourself.
For example, if the data is a Cursor and you place it in a
CursorAdapter, use the CursorAdapter(android.content.Context,
android.database.Cursor, int) constructor without passing in either
FLAG_AUTO_REQUERY or FLAG_REGISTER_CONTENT_OBSERVER (that is, use 0
for the flags argument). This prevents the CursorAdapter from doing
its own observing of the Cursor, which is not needed since when a
change happens you will get a new Cursor throw another call here.
But the Log.info line in the onLoadFinished method was not executed and listView didn't refreshed. Here is my (simple) code:
public class HomeActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>{
static final String TAG = "HomeActivity";
SimpleCursorAdapter adapter;
ListView listAnnunciVicini;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
listAnnunciVicini = (ListView) findViewById(R.id.lista_annunci_vicini);
adapter = new SimpleCursorAdapter(this, R.layout.list_item, null,
new String[] {
ContentDescriptor.Annunci.Cols.ID,
ContentDescriptor.Annunci.Cols.TITOLO,
ContentDescriptor.Annunci.Cols.DESCRIZIONE
}, new int[] {
R.id.list_annunci_item_id_annuncio,
R.id.list_annunci_item_titolo_annuncio,
R.id.list_annunci_item_descrizione_annuncio
}, 0);
listAnnunciVicini.setAdapter(adapter);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getSupportLoaderManager().initLoader(0, null, this).forceLoad();
}
public void addRandomItem(View sender) {
ContentValues dataToAdd = new ContentValues();
dataToAdd.put(ContentDescriptor.Annunci.Cols.TITOLO, "Titolo");
dataToAdd.put(ContentDescriptor.Annunci.Cols.DESCRIZIONE, "Lorem ipsum dolor sit amet.");
this.getContentResolver().insert(ContentDescriptor.Annunci.CONTENT_URI, dataToAdd);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// creating a Cursor for the data being displayed.
String[] proiezione = new String[] {ContentDescriptor.Annunci.Cols.ID, ContentDescriptor.Annunci.Cols.TITOLO, ContentDescriptor.Annunci.Cols.DESCRIZIONE };
CursorLoader cl = new CursorLoader(this, ContentDescriptor.Annunci.CONTENT_URI, proiezione, null, null, null);
return cl;
}
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.)
adapter.swapCursor(data);
Log.i(TAG, "I dati sono stati ricaricati");
}
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.
adapter.swapCursor(null);
}
}
Any suggestion?
AFAIK, you need to implement notification yourself in ContentProvider. For this, add something like getContext().getContentResolver().notifyChange(uri, null); to insert,update and delete method of ContentProvider and invoke.setNotificationUri(getContext().getContentResolver(), uri) on your Cursor in queryas described in official documentation. Here you can find the whole picture.
Note: You should not close the cursor (cursor.close()) in order to get
notifications about the changes.

Categories

Resources