I am trying to insert record into a sqlite database and bypassing the contentResolver. The first class named EventProvider extends ContentProvider and also contains an instance of my SQLite database helper class as shown below. So the code in the first class works fine when i insert records through a contentResolver but within the EventJSONParser class i want to insert the record directly the sqlite instead it throughs a NullPointerException in the SQLiteDatabase db = provider.getDatabase(); line of code within the unmarshalEventQuery method. I suspect that an instance of the databasehelper is not created by the onCreate() method. Please i need suggestions on how to solve this problem. thanks.
First Class:
public class EventProvider extends ContentProvider{
private DatabaseHelper helper;
private SQLiteDatabase db;
@Override
public boolean onCreate() {
helper = new DatabaseHelper(getContext());
db = helper.getWritableDatabase();
return true;
}
public SQLiteDatabase getDatabase(){
return db;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
if (BuildConfig.DEBUG) { Log.d(TAG, "insert@" + uri); }
switch (uriMatcher.match(uri)){
case EVENTS_DIR:
break;
default:
throw new UnsupportedOperationException("Unrecognized URI: " + uri);
}
values = COLUMN_MAP.translateCols(values);
values.put(DatabaseContract.COL_DIRTY, MARK);enter code here
SQLiteDatabase db = getDatabase();
return eventInsert(uri, values, db);
}
public Uri eventInsert(Uri uri, ContentValues values, SQLiteDatabase mdb){
if (BuildConfig.DEBUG){
Log.d(TAG, "insert@" + uri + ": {" + values + "}");
}
long pk = mdb.insert(DatabaseContract.EVENT_TABLE, null, values);
if (pk > 0) {
uri = null;
} else{
uri = uri.buildUpon().appendPath(String.valueOf(pk)).build();
getContext().getContentResolver().notifyChange(uri, null);
}
return uri;
}
Second class:
public class EventJSONParser {
private EventProvider provider;
public EventJSONParser(EventProvider restfulProvider){
provider = restfulProvider;
}
public void unmarshalEventQuery(JSONObject json){
try {
String error = json.getString(Const.KEY_ERROR);
int statusCode = json.getInt(Const.KEY_STATUS_CODE);
if (error.equals("false") && statusCode == 200){
JSONArray events = json.getJSONArray(TAG_EVENTS);
for(int i = 0; i < events.length(); i++){
JSONObject e = events.getJSONObject(i);
ContentValues values = new ContentValues();
values.put(UNMARSHAL_TAB.get(TAG_EVENT_ID), e.getString(TAG_EVENT_ID));
values.put(UNMARSHAL_TAB.get(TAG_CAL_ID), e.getString(TAG_CAL_ID));
values.put(UNMARSHAL_TAB.get(TAG_EVENT_TITLE), e.getString(TAG_EVENT_TITLE));
values.put(UNMARSHAL_TAB.get(TAG_START_DATE), e.getString(TAG_START_DATE));
values.put(UNMARSHAL_TAB.get(TAG_END_DATE), e.getString(TAG_END_DATE));
values.put(UNMARSHAL_TAB.get(TAG_LOCATION), e.getString(TAG_LOCATION));
values.put(UNMARSHAL_TAB.get(TAG_VISIBILITY), e.getString(TAG_VISIBILITY));
values.put(UNMARSHAL_TAB.get(TAG_DESCRIPTION), e.getString(TAG_DESCRIPTION));
SQLiteDatabase db = provider.getDatabase();
Uri uri = provider.eventInsert(EventContract.EVENT_URI, values, db);
Log.e(TAG, "query uri" + uri);
}
}
}catch (JSONException ex){
ex.printStackTrace();
}
}
}
Aucun commentaire:
Enregistrer un commentaire