注册 | 登录

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

itPublisher 分享于





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);


These are the overridden methods from LoaderCallbacks<Cursor>

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 );

public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {

public void onLoaderReset(Loader<Cursor> arg0) {
    //Not really sure how this works. Maybe I need to fix this here?


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


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

    推荐:Android Loader(三) 结合CursorLoader分析Loader相关源码

    Android Loader(二) CursorLoader Android Loader(四) 自定义Loader从网络中获取文本数据 首先从加载数据的过程开始分析。 初始化Loader的方法是:getLoaderMana

  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:

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri baseUri = SmartCalProvider.CONTENT_URI;
            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() }, 
        return new CursorLoader(
                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

推荐:android CursorLoader用法介绍

原文: android CursorLoader用法介绍 工作内容集中到Contact模块








您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱