我怎样才能刷新从CursorLoader光标?光标、CursorLoader

2023-09-06 01:37:04 作者:原谅我人丑心狠无人疼@

所以,我有我的 MainDisplayActivity 这同时实现了活动 LoaderManager.LoaderCallbacks<光标&GT ; 。在这里,我有一个的ListView ,我充满了我从我的数据库使用的ContentProvider获得议程的信息。我也有一个的GridView 这是一个日历。我把它单击单元格时,该议程与天点击更新设置。我的问题是重复使用的程序的时候,我在的onCreate()创建的setOnItemClickListener()内,它并没有刷新新的光标,我创建的信息。我可以创建一个新的Loader与另一个ID,和它的作品一次,但一旦我点击一天,它停止耳目一新。这个问题,在于光标。我怎样才能刷新从装载机光标所以我没有继续创造一个新的Loader?在此先感谢!

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!

初​​始调用我的 MainDisplayActivity 类创建的onCreate()装载程序:

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

这些都是从覆盖的方法 LoaderCallbacks&LT;光标&GT;

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

需要,请不要犹豫,问任何额外的code。再次感谢您的帮助!

Any additional code needed please do not hesitate to ask. Thanks again for any help!

推荐答案

您只需将您的code绕了一点,叫 restartLoader 。即,

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

当用户点击列表项,你以某种方式改变 getChosenDate返回的私有实例变量()(我是故意含糊这里,因为你没有具体说明究竟 getChosenDate()返回)。

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

一旦更改时,调用 getLoaderManager()。restartLoader()装载机。您的旧数据​​将被丢弃, restartLoader 将触发 onCreateLoader 来再次调用。这一次, getChosenDate()将返回不同的值(根据列表项,这是点击),而应该这样做。你的 onCreateLoader 的实施应该是这个样子:

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