我已经实现了自己的Loader并尝试在片段中使用它。
我知道ContentProvider代码正在运行,因为我已经使用LOG消息跟踪它。错误必须在我的片段代码中。
有人比我更有经验,请发现故意的错误。
public class StorePickerFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor> {
private SimpleCursorAdapter adapter;
/** Called when the Fragment is first created. */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fillData();
// Inflate the layout for this fragment
return inflater.inflate(R.layout.store_list, container, false);
}
private void fillData() {
// Fields from the database (projection)
// Must include the _id column for the adapter to work
String[] from = new String[] {StoreTable.COLUMN_ID, StoreTable.COLUMN_NAME };
// Fields on the UI to which we map
int[] to = new int[] { R.id.label };
getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(getActivity(), R.layout.store_row, null, from,
to, 0);
setListAdapter(adapter);
}
// Creates a new loader after the initLoader () call
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { StoreTable.COLUMN_ID, StoreTable.COLUMN_NAME };
CursorLoader cursorLoader = new CursorLoader(getActivity(),
MyContentProvider.STORE_URI, projection,null,null, StoreTable.COLUMN_NAME);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// data is not available anymore, delete reference
adapter.swapCursor(null);
}
}