Custom ContentProvider loading, but not firing onCreate()?

Up vote 0 down vote favorite share g+ share fb share tw.

I have 5 Content Providers for my 5 table SQLiteDatabase. They are named: CoreActionProvider CoreMovementProvider CoreRoundsProvider CoreChecksProvider CoreTermsProvider I have stepped through the startup process several times and have confirmed that all 5 Providers are loading. However, only CoreTermsProvider and CoreChecksProvider are firing their onCreate() methods.

The tables do not exist for for the other 3. I have attempted to run queries against them, and been greeted with the dreaded SQLException, stating that the table does not exist. Is there a limit to the number of custom providers?

I will post the code for CoreTermsProvider only, as all 5 providers share the same code structure, just different table names: package com.vortex.rules. Providers; import android.content. ContentProvider; import android.content.

ContentUris; import android.content. ContentValues; import android.content. Context; import android.content.

UriMatcher; import android.database. Cursor; import android.database. SQLException; import android.database.sqlite.

SQLiteDatabase; import android.database.sqlite. SQLiteOpenHelper; import android.database.sqlite. SQLiteQueryBuilder; import android.net.

Uri; import android.text. TextUtils; import android.util. Log; public class CoreTermsProvider extends ContentProvider{ public static final String PROVIDER_NAME = "com.vortex.rules.providers.

CoreTerms"; public static final Uri CONTENT_URI = Uri. Parse("content://"+ PROVIDER_NAME + "/terms"); public static final String _ID = "_id"; public static final String TITLE = "title"; public static final String TEXT = "text"; private static final int TERMS = 1; private static final int TERM_ID = 2; private static final UriMatcher uriMatcher; static{ uriMatcher = new UriMatcher(UriMatcher. NO_MATCH); uriMatcher.

AddURI(PROVIDER_NAME, "terms", TERMS); uriMatcher. AddURI(PROVIDER_NAME, "terms/#", TERM_ID); } //---for database use--- private SQLiteDatabase rulesDB; private static final String DATABASE_NAME = "Rules. Db"; private static final String DATABASE_TABLE = "terms"; private static final int DATABASE_VERSION = 1; private static final String DATABASE_CREATE = "create table if not exists " + DATABASE_TABLE + "(_id integer primary key autoincrement, " + "title text not null, text longtext not null);"; private static class DatabaseHelper extends SQLiteOpenHelper{ DatabaseHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db){ db.

ExecSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ Log. W("Content Provider Database", "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); db. ExecSQL("DROP TABLE IF EXISTS terms"); onCreate(db); } } @Override public String getType(Uri uri){ switch (uriMatcher.

Match(uri)){ //---get all terms--- case TERMS: return "vnd.android.cursor. Dir/vnd.vortex.rules. Terms "; //---get a particular book case TERM_ID: return "vnd.android.cursor.

Item/vnd.vortex.rules. Terms "; default: throw new IllegalArgumentException("Unsupported URI: " + uri); } } @Override public boolean onCreate(){ Context context = this.getContext(); DatabaseHelper dbHelper = new DatabaseHelper(context); rulesDB = dbHelper. GetWritableDatabase(); return (rulesDB == null)?

False:true; } @Override public Cursor query(Uri uri, String projection, String selection, String selectionArgs, String sortOrder){ SQLiteQueryBuilder sqlBuilder = new SQLiteQueryBuilder(); sqlBuilder. SetTables(DATABASE_TABLE); if (uriMatcher. Match(uri) == TERM_ID) //---if getting a particular term--- sqlBuilder.

AppendWhere(_ID + "=" + uri.getPathSegments(). Get(1)); if (sortOrder == null || sortOrder=="") sortOrder = TITLE; Cursor c = sqlBuilder. Query(rulesDB, projection, selection, selectionArgs, null, null, sortOrder); //---register to watch a content URI for changes--- c.

SetNotificationUri(getContext(). GetContentResolver(), uri); return c; } @Override public Uri insert(Uri uri, ContentValues values){ //---add a new term--- long rowId = rulesDB. Insert(DATABASE_TABLE, "", values); //---if added successfully--- if (rowId > 0) { Uri _uri = ContentUris.

WithAppendedId(CONTENT_URI, rowId); getContext(). GetContentResolver(). NotifyChange(_uri, null); return _uri; } throw new SQLException("Failed to insert row into " + uri); } @Override public int delete(Uri arg0, String arg1, String arg2){ // arg0 = uri // arg1 = selection // arg2 = se;ectionArgs int count = 0; switch (uriMatcher.

Match(arg0)) { case TERMS: count = rulesDB. Delete(DATABASE_TABLE, arg1, arg2); break; case TERM_ID: String id = arg0.getPathSegments(). Get(1); count = rulesDB.

Delete(DATABASE_TABLE, _ID + "=" + id + (!TextUtils. IsEmpty(arg1)? " AND (" + arg1 + ')' : ""), arg2); break; default: throw new IllegalArgumentException("Unknown URI: " + arg0); } getContext().

GetContentResolver(). NotifyChange(arg0, null); return count; } @Override public int update(Uri uri, ContentValues values, String selection, String selectionArgs) { int count = 0; switch (uriMatcher. Match(uri)){ case TERMS: count = rulesDB.

Update(DATABASE_TABLE, values, selection, selectionArgs); break; case TERM_ID: count = rulesDB. Update(DATABASE_TABLE, values, _ID + "=" + uri.getPathSegments(). Get(1) + (!TextUtils.

IsEmpty(selection)? " AND (" + selection + ')' : ""), selectionArgs); break; default: throw new IllegalArgumentException("Unknown URI: " + uri); } getContext(). GetContentResolver().

NotifyChange(uri, null); return count; } } As you can see, it is a basic Content Provider. Any help on this issue would be greatly appreciated. I would have posted this on StackOverflow, but the "Ask A Question" link was throwing errors.

Sincerely, Michael Martin android contentprovider oncreate android-contentprovider link|improve this question asked Aug 1 '10 at 22:12Michael Martin13.

I have stepped through the startup process several times and have confirmed that all 5 Providers are loading. However, only CoreTermsProvider and CoreChecksProvider are firing their onCreate() methods. " -- how are you determining they are loading, if onCreate() is not being invoked?

Also, why do you have five providers, when you only need one, with separate CONTENT_URI values for each? – CommonsWare Aug 1 '10 at 22:18 If I stick a breakpoint at "public class...." eclipse breaks at each provider during startup. I can only assume that this means that the classes are being loaded.

I will look into providing seperate CONTENT_URI's. – Michael Martin Aug 2 '10 at 16:33 ok.....so I changed to one ContentProvider with multiple CONTENT_URI's. The tables are being created.

I check each table individually to see if they have data (they shouldn't on first creation) the code just skips over the tables, except the first check. CODE: if ((cr. Query(CoreProvider.

TERM_CONTENT_URI, projection, null, null, null)).moveToFirst() == false) { CreateDatabase(R.raw. Terms, CoreProvider. TERM_CONTENT_URI); } This block is follwed immediatly by 4 more, for each table with different CONTENT_URI's.

Each table has the same baxse structure. – Michael Martin Aug 3 '10 at 1:10 the terms table fails the check, so it calls the CreateDatabase(R.raw. Terms, CoreProvider.

TERMS_CONTENT_URI); and that completes successfully. It the moves on to the next if block, which checks a new table, but the table passes the check. It should fail.

– Michael Martin Aug 3 '10 at 1:12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions