1

我有一个带有 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);
    }
}

关于为什么永远不会调用此查询方法的任何想法?谢谢!

4

1 回答 1

2

正如@nandeesh 在评论中提到的,通过在清单文件中声明您的提供者来解决这个问题。

它应该在表格上:

<manifest>
    ...
    <application>
        <activity>...</activity>

    ...
    <provider ... />

    </application>
</manifest>

我能够在我的项目中重现该问题。我希望它可以被检测为某种警告。但是如果有人遇到查询没有被调用,在我的例子中,并不是所有的 ContentProvider 接口方法都被调用过,这是因为我还没有在 Manifest 文件中声明提供者。

我希望这对将来的其他人有所帮助。

于 2015-04-07T07:39:00.133 回答