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

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.

Related

How to make notifyChange() work between two activities?

I have an activity ActitvityA that holds a listview populated by a CursorLoader. I want to switch to ActivityB and change some data and see those changes reflected in listview in ActivityA.
public class ActivityA implements LoaderManager.LoaderCallbacks<Cursor>
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
getSupportLoaderManager().initLoader(LOADER_ID, null, this);
mCursorAdapter = new MyCursorAdapter(
this,
R.layout.my_list_item,
null,
0 );
}
.
.
.
/** Implementation of LoaderManager.LoaderCallbacks<Cursor> methods */
#Override
public Loader<Cursor> onCreateLoader(int loaderId, Bundle arg1) {
CursorLoader result;
switch ( loaderId ) {
case LOADER_ID:
/* Rename v _id is required for adapter to work */
/* Use of builtin ROWID http://www.sqlite.org/autoinc.html */
String[] projection = {
DBHelper.COLUMN_ID + " AS _id", //http://www.sqlite.org/autoinc.html
DBHelper.COLUMN_NAME // columns in select
}
result = new CursorLoader( ActivityA.this,
MyContentProvider.CONTENT_URI,
projection,
null,
new String[] {},
DBHelper.COLUMN_NAME + " ASC");
break;
default: throw new IllegalArgumentException("Loader id has an unexpectd value.");
}
return result;
}
/** Implementation of LoaderManager.LoaderCallbacks<Cursor> methods */
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
switch (loader.getId()) {
case LOADER_ID:
mCursorAdapter.swapCursor(cursor);
break;
default: throw new IllegalArgumentException("Loader has an unexpected id.");
}
}
.
.
.
}
From ActivityA I switch to ActivityB where I change the underlying data.
// insert record into table TABLE_NAME
ContentValues values = new ContentValues();
values.put(DBHelper.COLUMN_NAME, someValue);
context.getContentResolver().insert( MyContentProvider.CONTENT_URI, values);
The details of MyContentProvider:
public class MyContentProvider extends ContentProvider {
.
.
.
#Override
public Uri insert(Uri uri, ContentValues values) {
int uriCode = sURIMatcher.match(uri);
SQLiteDatabase database = DBHelper.getInstance().getWritableDatabase();
long id = 0;
switch (uriType) {
case URI_CODE:
id = database.insertWithOnConflict(DBHelper.TABLE_FAVORITE, null, values,SQLiteDatabase.CONFLICT_REPLACE);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null); // I call the notifyChange with correct uri
return ContentUris.withAppendedId(uri, id);
}
#Override
public Cursor query(Uri uri,
String[] projection,
String selection,
String[] selectionArgs,
String sortOrder) {
// Using SQLiteQueryBuilder instead of query() method
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
int uriCode = sURIMatcher.match(uri);
switch (uriCode) {
case URI_CODE:
// Set the table
queryBuilder.setTables(DBHelper.TABLE_NAME);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
SQLiteDatabase database = DBHelper.getInstance().getWritableDatabase();
Cursor cursor = queryBuilder.query( database, projection, selection, selectionArgs, null, null, sortOrder);
// Make sure that potential listeners are getting notified
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
}
As far as my knowledge goes this should be sufficient. But it does not work. Upon returning to ActivityA the listview is unchanged.
I have followed things with debugger and this is what happens.
First visit ActivityA, the methods that are called in that order
MyContentProvider.query()
ActivityA.onLoadFinished()
The listview displays the correct values.
And now I switch to activityB and change the data
MyContentProvider.insert() // this one calls getContext().getContentResolver().notifyChange(uri, null);
MyContentProvider.query()
//As we can see the MyContentProvider.query is executed. I guess in response to notifyChange().
// What I found puzzling why now, when ActivityB is still active ?
Return to ActivityA
!!! ActivityA.onLoadFinished() is not called
I have read anything I could about this, took a close look at a lot of stackoverflow questions yet all those question/answers revolve around setNotificationUri() and notifyChangeCombo() which I implemented. Why does this not work across activities?
If for example force refresh in ActivityA.onResume() with
getContentResolver().notifyChange(MyContentProvider.CONTENT_URI, null, false);
then it refreshes the list view. But that would force refresh on every resume regardless if data was changed or not.
After a two long two days of scratching my head and altruistic engagement from pskink I painted myself a picture of what was wrong.
My ActivityA is in a reality a lot more complicated. It uses ViewPager with PagerAdapter with instantiates listviews.
At first I created those components in onCreate() method something like this:
#Override
public void onCreate(Bundle savedInstanceState)
{
...
super.onCreate(savedInstanceState);
// 1 .ViewPager
viewPager = (ViewPager) findViewById(R.id.viewPager);
...
viewPager.setAdapter( new MyPagerAdapter() );
viewPager.setOnPageChangeListener(this); */
...
// 2. Loader
getSupportLoaderManager().initLoader(LOADER_ID, null, this);
...
// 3. CursorAdapter
myCursorAdapter = new MyCursorAdapter(
this,
R.layout.list_item_favorites_history,
null,
0);
}
Somewhere along the line I noticed that this is wrong order of creating. Why it didn't produce some error is because PagerAdapter.instantiateItem() is called aftter onCreate() finishes. I dont know why or how this caused the original problem. Maybe something did not wire correctly with listviews, adapters and content observers. I didn't dig into that.
I changed the order to:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
...
// 1. CursorAdapter
myCursorAdapter = new MyCursorAdapter(
this,
R.layout.list_item_favorites_history,
null,
0);
...
// 2. Loader
getSupportLoaderManager().initLoader(LOADER_ID, null, this);
...
// 3 .ViewPager
viewPager = (ViewPager) findViewById(R.id.viewPager);
...
viewPager.setAdapter( new MyPagerAdapter() );
viewPager.setOnPageChangeListener(this); */
...
}
This magically made it work in about 75% of the cases. When I studied CatLog output I noticed that ActivityA().onStop() is called at different times. When it works it is called late and I can see in logcat that onLoadFinished() executes. Sometimes ActivityA.onStop() executes right after query and then onLoadFinished() is not called at all. This brings me to what DeeV jas posted in his answer about cursors being unregistered from ContentResolver. This just might be the case.
What made things to somehow came to light was the fact that simple demonstrator pskink insisted on did work and my app didn't although they were identical in key points. This brought my attention to asynchronous things and my onCreate() method. In reality my ActivityB is complicated so it gives enough time for ActivityA to stop.
What I noticed also (and this did make things more difficult to sort) was that if I run my 75% version in debug mode (with no breakpoints) then the success rate falls to 0. ActivityA is stopped before cursor load finishes so my onLoadFinished() is never called and listviews are never updated.
Two key points:
Somehow the order of creation od ViewPager, CursorAdapter and
CursorLoader is important
ActivityA may be (and is) stopped before
cursor is loaded.
But even this is not. If I take a look at a sequence of simplified then I see that ActivityA.onStop() is executed before content provider inserts a record. I see no query while ActivityB is active. But when i return to ActivityA a query is execeuted laodFinished() follows and listview is refreshed. Not so in my app. It always executes a query while still in ActivityB, why??? This destroy my theory about onStop() being the culprit.
(Big thanks to pskink and DeeV)
UPDATE
After a lot of waisted time on this issue I finally nailed the cause of the problem.
Short description:
I have the following classes:
ActivityA - contains a list view populated via cursor loader.
ActivityB - that changes data in database
ContentProvider - content provider used for data manipulation and also used by cursorloader.
The problem:
After data manipulation in ActivityB the changes are not shown in list view in ActivityA. List view is not refreshed.
After I lot of eyeballing and studying logcat traces I have seen that things proceed in following sequence:
ActivityA is started
ActivityA.onCreate()
-> getSupportLoaderManager().initLoader(LOADER_ID, null, this);
ContentProvider.query(uri) // query is executes as it should
ActivityA.onLoadFinished() // in this event handler we change cursor in list view adapter and listview is populated
ActivityA starts ActivityB
ActivityA.startActivity(intent)
ActivityB.onCreate()
-> ContentProvider.insert(uri) // data is changed in the onCreate() method. Retrieved over internet and written into DB.
-> getContext().getContentResolver().notifyChange(uri, null); // notify observers
ContentProvider.query(uri)
/* We can see that a query in content provider is executed.
This is WRONG in my case. The only cursor for this uri is cursor in cursor loader of ActivityA.
But ActivityA is not visible any more, so there is no need for it's observer to observe. */
ActivityA.onStop()
/* !!! Only now is this event executed. That means that ActivityA was stopped only now.
This also means (I guess) that all the loader/loading of ActivityA in progress were stopped.
We can also see that ActivityA.onLoadFinished() was not called, so the listview was never updated.
Note that ActivityA was not destroyed. What is causing Activity to be stopped so late I do not know.*/
ActivityB finishes and we return to ActivityA
ActivityA.onResume()
/* No ContentProvider.query() is executed because we have cursor has already consumed
notification while ActivityB was visible and ActivityA was not yet stopped.
Because there is no query() there is no onLoadFinished() execution and no data is updated in listview */
So the problem is not that ActivityA is stopped to soon but that it is stopped to late. The data is updated and notification
sent somewhere between creation of ActivityB and stopping of ActivityA.
The solution is to force loader in ActivityA to stop loading just before ActivityB is started.
ActivityA.getSupportLoaderManager().getLoader(LOADER_ID).stopLoading(); // <- THIS IS THE KEY
ActivityA.startActivity(intent)
This stops the loader and (I guess again) prevents cursor to consume notification while activity is in the above described limbo state.
The sequence of events now is:
ActivityA is started
ActivityA.onCreate()
-> getSupportLoaderManager().initLoader(LOADER_ID, null, this);
ContentProvider.query(uri) // query is executes as it should
ActivityA.onLoadFinished() // in this event handler we change cursor in list view adapter and listview is populated
ActivityA starts ActivityB
ActivityA.getSupportLoaderManager().getLoader(LOADER_ID).stopLoading();
ActivityA.startActivity(intent)
ActivityB.onCreate()
-> ContentProvider.insert(uri)
-> getContext().getContentResolver().notifyChange(uri, null); // notify observers
/* No ContentProvider.query(uri) is executed, because we have stopped the loader in ActivityA. */
ActivityA.onStop()
/* This event is still executed late. But we have stopped the loader so it didn't consume notification. */
ActivityB finishes and we return to ActivityA
ActivityA.onResume()
ContentProvider.query(uri) // query is executes as it should
ActivityA.onLoadFinished() // in this event handler we change cursor in list view adapter and listview is populated
/* The listview is now populated with up to date data */
This was the most elegant solution I could find. No need to restart loaders and such.
But still I would like to hear a comment on that subject from someone with a deeper insight.
I don't see anything here particularly wrong with this. As long as the Cursor is registered with the URI, the loader should be restarting itself with new information. I don't think the issue here is anything wrong with your code. I think it's the LoaderManager is unregistering the Cursor from the ContentResolver too early (it actually happens by the time onStop() is called).
Basically there's nothing you can really do about it unregistering. You can however, force restart the loader by calling LoaderManager#restartLoader(int, Bundle, LoaderCallbacks);. You can call this in onStart() (which makes the initLoader call in onCreate() useless). A more optimized approach would be to use onActivityResult(). The result of your activity is irrelevant in this case. All you're saying is that you've returned to this activity from some other activity and the data may or may not be different, so you need to reload.
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
getSupportLoaderManager().restartLoader(LOADER_ID, null, this);
}
Then just call Context#startActivityForResult() when opening new Activities.

ActiveAndroid get data from two tables with CursorLoader (custom ContentProvider?)

In active android there is option to get data from database using CursorLoader, but in example there is only option with 1 table:
MyActivity.this.getSupportLoaderManager().initLoader(0, null, new LoaderCallbacks<Cursor>() {
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle cursor) {
return new CursorLoader(MyActivity.this,
ContentProvider.createUri(TodoItem.class, null),
null, null, null, null
);
}
// ...
});
I want to make join, in normal case i would make uri in content provider for joined tables, but ActiveAndroid delivers ContentProvider, and i dont know how should i do it. Is there option to use custom ContentProvider with ActiveAndroid? Or is there other option?

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 do CursorLoader automatically updates the view even if the app is inactive?

I have been working on a small To-Do list app. I used CursorLoader to update the ToDolistview from a content provider. I have a written a function onNewItemAdded(), which is called when user enters a new item in the text view and clicks enter. Refer below:
public void onNewItemAdded(String newItem) {
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(ToDoContentProvider.KEY_TASK, newItem);
cr.insert(ToDoContentProvider.CONTENT_URI, values);
// getLoaderManager().restartLoader(0, null, this); // commented for the sake of testing
}
#Override
protected void onResume() {
super.onResume();
//getLoaderManager().restartLoader(0, null, this); // commented for the sake of testing
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
CursorLoader loader = new CursorLoader(this,
ToDoContentProvider.CONTENT_URI, null, null, null, null);
Log.e("GOPAL", "In the onCreateLoader");
return loader;
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
int keyTaskIndex = cursor.getColumnIndexOrThrow(ToDoContentProvider.KEY_TASK);
Log.e("GOPAL", "In the onLoadFinished");
todoItems.clear();
if (cursor.moveToNext() == false) Log.e("GOPAL", "Empty Cursor");
else {
while (cursor.moveToNext()) {
ToDoItem newItem = new ToDoItem(cursor.getString(keyTaskIndex));
todoItems.add(newItem);
}
aa.notifyDataSetChanged(); // aa is arrayadapter used for the listview
}
}
I have read, CursorLoader automatically updates the view, whenever there is a data change in the content provider db. That means I suppose, getLoaderManager().restartLoader(0, null, this) has to be called implicitly whenever there is a change in data, right?
But that is not happening. Whenever I add a new item (the item is added to the db from onNewItemAdded, but restartLoader is not explicitly called), pause this activity and resume it back. I don't see any implicit call to restartLoader(even though db is changed) and the listview also is not updated with new item added. Why is that? How does a CursorLoader automatically updates the view even if app is not active???
Thanks :)
EDIT: I have also used getContext().getContentResolver().notifyChange(insertedId, null) in insert of my content provider.
I found the answer for my question. In general, CursorLoader doesn't automatically detect data changes and load them to view. We need to track URI for changes. This can be done by following steps:
Registering an Observer in content resolver through cursor using: (Done in the query method of ContentProvider)
cursor.setNotificationUri(getContext().getContentResolver(), uri);
Now when there is any change in URI underlying data using insert()/delete()/update(), we notify the ContentResolver about the change using:
getContext().getContentResolver().notifyChange(insertedId, null);
This is received by the observer, we registered in step-1 and this calls to ContentResolver.query(), which inturn calls ContentProvider's query() method to return a fresh cursor to LoaderManager. LoaderManager calls onLoadFinished() passing this cursor, along with the CursorLoader where we update the View (using Adapter.swapCursor()) with fresh data.
For Custom AsyncTaskLoaders:
At times we need our custom loader instead of CursorLoader. Here we can use someother object other than cursor to point to the loaded data (like list etc). In this we won't be having previlige to notify ContentResolver through cursor. The application may also not have a content Provider, to track URI changes. In this scenario we use BroadcastReceiver or explicit ContentObserver to achieve automatic view updation. This is as follows:
We need to define our custom loader which extends AsyncTaskLoader and implements all its abstract methods. Unlike CursorLoader, our Custom Loader may or may not use a content Provider and it's constructor may not call to ContentResolver.query(), when this loader is instatiated. So we use a broadcast receiver to serve the purpose.
We need to instantiate a BroadCastReceiver or ContentObserver in OnStartLoading() method of abstract AsyncTaskLoader class.
This BroadCast receiver should be defined to receive data-changing broadcasts from content provider or any system events(Like new app installed) and it has to call loader's onContentChanged() method, to notify the loader about the data change. Loader automatically does the rest to load the updated data and call onLoadFinished() to update the view.
For more details refer this: http://developer.android.com/reference/android/content/AsyncTaskLoader.html
I found this very useful for clear explanation : http://www.androiddesignpatterns.com/2012/08/implementing-loaders.html
Well, I think you can restart the loader on certain events. E.g. in my case I have an activity of TODOs. On clicking 'add' option, it launches new activity which has view to feed new TODO.
I am using following code in parent activity's onActivityResult()
getLoaderManager().restartLoader(0, null, this);
It works fine for me. Please share if there is any better approach.
get a reference to your loader while initializing as follows
Loader dayWeatherLoader = getLoaderManager().initLoader(LOADER_DAY_WEATHER, null, this);
then create a class that extends ContentObserver as follows
class DataObserver extends ContentObserver {
public DataObserver(Handler handler) {
super(handler);
}
#Override
public void onChange(boolean selfChange, Uri uri) {
dayWeatherLoader.forceLoad();
}
}
Then register content observer inside onResume lifecycle method as follows
#Override
public void onResume() {
super.onResume();
getContext().getContentResolver().registerContentObserver(CONTENTPROVIDERURI,true,new DayWeatherDataObserver(new Handler()));
}
Whenever there is a change in the underlying data of content provider, the onChange method of contentobserver will be called where you can ask loader to load the data again

How can I refresh the cursor from a CursorLoader?

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. :)

Categories

Resources