I'm just starting to use ContentProviders in my project so forgive the newbie question. Also in case it's relevant I'm using this tutorial as reference.
From what I understand I have to report to my ContentProvider of any CRUD operations for it to notify any other area in my project of those changes. But my problem is my CRUD operations are all inside my DatabaseHelper.class along with my queries and such. So when I try to use getContentResolver().CRUD(uri, cv, null, null) I get a mark over getContentResolver() with the message Cannot resolve method.
Now when I looked up on ContentResolver I found that I can only use it within a class that extends Activity, but since all my CRUD operations are held within my DatabaseHelper.class which extends SQLiteOpenHelper not Activity.
I'm not sure how to get around this if anyone can guide a beginner accordingly that would be greatly appreciated. Below are my CRUD operations in DatabaseHelper, if anything else is needed just ask:
Add
void AddAccount(Account acc) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(colName, acc.getName());
cv.put(colComp, acc.getComp());
cv.put(colAmount, acc.getAmt());
cv.put(colPurpose, acc.getPurpose());
cv.put(colTerms, acc.getTerms());
cv.put(colPeriod, acc.getPeriod());
cv.put(colBalance, acc.getAmt());
cv.put(colStatus, acc.getStatus());
cv.put(colDate, acc.getDate());
cv.put(colEditDate, acc.getEditDate());
db.insert(ACCOUNTS, colName, cv);
getContentResolver().insert(ContentProviderDB.CONTENT_URI, cv);
db.close();
}
Update
public int UpdateAcc(Account acc) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(colName, acc.getName());
cv.put(colComp, acc.getComp());
cv.put(colAmount, acc.getAmt());
cv.put(colPurpose, acc.getPurpose());
cv.put(colTerms, acc.getTerms());
cv.put(colPeriod, acc.getPeriod());
cv.put(colEditDate, acc.getEditDate());
cv.put(colStatus, acc.getStatus());
Uri uri = Uri.parse(ContentProviderDB.CONTENT_URI + "/" + acc.getID());
getContentResolver().update(uri, cv, null, null);
return db.update(ACCOUNTS, cv, colID + "=?", new String[]{String.valueOf(acc.getID())});
}
Delete
public void DeleteAcc(Account acc) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(ACCOUNTS, colID + "=?", new String[]{String.valueOf(acc.getID())});
Uri uri = Uri.parse(ContentProviderDB.CONTENT_URI + "/" + acc.getID());
getContentResolver().delete(uri, null, null);
db.close();
}
Aucun commentaire:
Enregistrer un commentaire