How can I refresh the cursor from a CursorLoader?

前端 未结 1 504
梦谈多话
梦谈多话 2020-12-29 21:08

So I have my MainDisplayActivity which implements both Activity and LoaderManager.LoaderCallbacks. Here I have A L

相关标签:
1条回答
  • 2020-12-29 22:09

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

    0 讨论(0)
提交回复
热议问题