Content Provider Does Not Sync with SqliteDatabase

I have 2 actions.

ActivityA accesses the database through the Content Provider and launches ActivityB .

ActivityB directly accesses the database.

I found that after ActivityB updated the database, ActivityA requested the database by CP, and the result will not be updated.

But the database is actually being updated!

How to synchronize two methods?

PS: ActivityA and ActivityB are in different applications.

+7
android
source share
3 answers

Content providers when you upgrade as a polite call to NotifyChange This means that the subscribers to your content provider have changed the content.

Activity A requires access to the content provider by URI and registration of the content observer. For B to work, you need to call NotifyChange using a row-based URI so that change observers get a row-based URI. It all depends on the URI used to access the contentprovider. This gives real-time changes to bA. B changes →> A receives the changes and can do something with them.

A notification / event to observe the row and column level in the content provider

Update or notify Fragment data in ViewPager

Content Provider Update

+2
source share

** database **

  @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, " + key_msg + " STRING, " + key_isread + " STRING)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } 

// ************************** ------- INSERT GCM MESSAGE --------- * *** *********************** //

 public void insert_GCM_receive_data(String msg) { String value; SQLiteDatabase db = getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(key_msg, msg); cv.put(key_isread, "N"); value = cv.toString(); db.insert(TABLE_NAME, null, cv); System.out.println("/n******this is temp table name " + TABLE_NAME + "\nthis is temp msg " + cv + "\nmsg" + msg + "\nval" + value); db.close(); } 

// --------------------------------------------- --- ----------------------------------- //

// *********************** ---------- GET WARNING DATA ---------- - * ************************** // public ArrayList get_alert_msg () {

  ArrayList<String> name = new ArrayList<String>(); try { SQLiteDatabase db = getWritableDatabase(); Cursor c = null; c = db.rawQuery("SELECT * FROM " + TABLE_NAME, null); System.out.println(c); for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { String str_id = c.getString(0); String str_msg = c.getString(1); String str_read = c.getString(2); Log.e("value", str_id + str_msg + str_read); HashMap<String, String> hm = new HashMap<String, String>(); hm.put("msg", str_msg); hm.put("isread", str_read); name.add(str_msg); } c.close(); db.close(); } catch (Exception e) { Log.e("this not work", "" + e); } return name; } 

// method call, as in 2 actions dbHelper.get_alert_msg (); ....

The operation of data manipulation and data extraction is used in a database class ... As soon as you close db after receiving the data. Use a common database method for two activities. Hope this helps you.

+2
source share

Be sure to call close() on the db cursors or helpers when the b operation is performed with updates.

-one
source share

All Articles