我在检查代码时发现我并不知道一个CursorLoader和LoaderManage组合起来如何与CursorAdapter连接。这就是我疑惑的部分:
agendaAdapter = new MyAgendaAdapter(this, null);
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);
我找不到连接,如何传递自定义的ContentProvider中的query()方法到指定的CursorAdapter?
1.首先,创建CursorAdapter
mAdapter = new SimpleCursorAdapter(
getActivity(),
android.R.layout.simple_list_item_2,
null,
new String[] { Contacts.DISPLAY_NAME, Contacts.CONTACT_STATUS },
new int[] { android.R.id.text1, android.R.id.text2 },
0);
2.然后,初始化loader。
getLoaderManager().initLoader(0, null, this);
3.LoaderManager调用onCreateLoader(int id, Bundle args).
4.查询光标传递到adapter。
5.查询数据与CursorAdapter连接起来。
LZ可以查看一下程序里LoaderManager.LoaderCallbacks实现部分,其中:
1.在onCreateLoader()方法里应该会新建CursorLoader;
2.在onLoadFinished()方法里会把由CursorLoader查得的Cursor传回给你,你这里的agendaAdapter应该就是传回的Cursor。