所以我有我的MainDisplayActivity
which 实现了Activity
和LoaderManager.LoaderCallbacks<Cursor>
。这里我有 A ListView
,我使用 ContentProvider 从我的数据库中获取的议程信息填充它。我也有GridView
一个日历。我在单击单元格时设置了它,议程会随着单击的日期更新。我的问题是,当重用我在onCreate()
setOnItemClickListener() 内部创建的加载器时,它不会用我正在创建的新光标刷新信息。我可以用另一个 ID 创建一个新的加载器,它可以工作一次,但是一旦我点击另一天,它就会停止刷新。问题出在光标上。如何从 Loader 刷新光标,这样我就不必继续创建新的 Loader?提前致谢!
MainDisplayActivity
在 My类的 onCreate() 中创建 Loader 的初始调用:
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<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);
}
需要任何其他代码,请随时询问。再次感谢任何帮助!