Search code examples
androidandroid-contentprovider

ContentProvider's query method is being called


I have an Activity with Fragments and a ContentProvider. For some reason, my ContentProvider's query method is never called - I've verified this in the Eclipse debugger. Here's my relevant Fragment code:

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);    
}

And here's the relevant ContentProvider query. SQLData is my 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);
    }
}

Any ideas on why this query method is never called? Thanks!


Solution

  • As @nandeesh mentioned on the comment, fix this by declaring your provider in the manifest file.

    It should be on the form:

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

    I was able to reproduce the problem on my project. I hope it could be detected as some sort warning though. But in case someone encountered query has not been called, in my case not all of the ContentProvider interface method ever called, this is because I have not declared the provider yet on the Manifest file.

    I hope this well help other people in the future.