我有一个带有 Fragments 和 ContentProvider 的 Activity。出于某种原因,我的 ContentProvider 的查询方法从未被调用过——我已经在 Eclipse 调试器中验证了这一点。这是我的相关片段代码:
public class ListFrag extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private SimpleCursorAdapter mAdapter;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Intent myData = getActivity().getIntent();
Bundle info = myData.getExtras();
String[] dataColumns = { "stitchname" };
int[] viewIDs = { R.id.stitchlist1 };
mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.stitchlist, null, dataColumns, viewIDs, 0);
setListAdapter(mAdapter);
getLoaderManager().initLoader(0, info, (LoaderCallbacks<Cursor>) this);
}
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selection = "stitchlevel=?";
String[] selectionArgs = new String[] {args.getString("Level")};
return (Loader<Cursor>) new CursorLoader(getActivity(), STITCHES_URI,
PROJECTION, selection, selectionArgs, null);
}
这是相关的 ContentProvider 查询。SQLData 是我的 SQLiteDatabase:
public class StitchProvider extends ContentProvider {
private static final String STITCHTABLE_BASEPATH = "MyStitches_tbl";
public static final int STITCHES = 100;
public static final int STITCHES_ID = 110;
private SQLData mDB;
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(STITCHTABLE_BASEPATH);
int uriType = sURIMatcher.match(uri);
switch (uriType)
{
case STITCHES_ID:
queryBuilder.appendWhere(SQLData.KEY_ROWID + "=" + uri.getLastPathSegment());
break;
case STITCHES:
//no filter
break;
default:
throw new IllegalArgumentException("Unknown URI");
}
Cursor cursor = queryBuilder.query(mDB.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
sURIMatcher.addURI(AUTHORITY, STITCHTABLE_BASEPATH, STITCHES);
sURIMatcher.addURI(AUTHORITY, STITCHTABLE_BASEPATH + "/#", STITCHES_ID);
}
}
关于为什么永远不会调用此查询方法的任何想法?谢谢!