How can I refresh the cursor from a CursorLoader?

19,550

You just need to move your code around a little and call restartLoader. That is,

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

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

Share:
19,550

Related videos on Youtube

Andy
Author by

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, 2020

Comments

  • Andy
    Andy almost 4 years

    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!

    • stuckless
      stuckless almost 12 years
      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?
    • Andy
      Andy almost 12 years
      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
    Andy almost 12 years
    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
    Andy almost 12 years
    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!
  • Alex Lockwood
    Alex Lockwood almost 12 years
    Haha... you are easily excitable, aren't you. Glad I could help :).
  • Andy
    Andy almost 12 years
    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.
  • Alex Lockwood
    Alex Lockwood almost 12 years
    That's what it felt like for me too. Glad it worked out for you!
  • Alex Lockwood
    Alex Lockwood almost 12 years
    @Andy, did you end up figuring this out?
  • Andy
    Andy almost 12 years
    I 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?