Android Data Cloud Sync (i) Generate Operation SQLite Record

Source: Internet
Author: User

Synchronous thinking:

This paper introduces the data synchronization problem of the client for the latest data, which is often applied to the cloud notes, which can be used to remember similar apps.

Here I built a ListView, the item on the ListView additions and deletions, to replace the actual situation in the project.


1. the ListView and Local Data Table DataTable table (using the ID as a unique representation of data) mapping, implementation of the increase and deletion.

2. Each time the operation of the local data table is recorded in the Operation table, the timestamp of the operation is recorded in the table.

3. Encapsulate the contents of the Operation table in JSON data to the server (get the last synchronization timestamp from the server and commit only after this update).

4. The server update completes, returns success, indicates the synchronization succeeds, at the time can empty the operation table


Mainactivity.java


Package Com.example.sqllitcache;import Java.text.simpledateformat;import Java.util.date;import Android.app.activity;import Android.app.alertdialog;import Android.content.contentvalues;import Android.content.dialoginterface;import Android.database.cursor;import android.database.sqlite.SQLiteDatabase; Import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.adapterview;import Android.widget.adapterview.onitemclicklistener;import Android.widget.adapterview.onitemlongclicklistener;import Android.widget.arrayadapter;import Android.widget.button;import Android.widget.edittext;import Android.widget.imagebutton;import Android.widget.listview;import Android.widget.toast;public class Mainactivity extends Activity {ListView lv; Button submit; EditText et; Sqlitedatabase dbwrite;D bhelper dbhelper; Arrayadapter <String> Adapter; ImageButton upload;    Cursor C; @Override protected void OnCreate (Bundle savedinstancestate) {Super.oncReate (savedinstancestate);        Setcontentview (R.layout.activity_main);        lv= (ListView) Findviewbyid (R.ID.LISTVIEW1);        submit= (Button) Findviewbyid (R.id.button1);        Upload= (ImageButton) Findviewbyid (R.id.imagebutton1);        et= (EditText) Findviewbyid (R.ID.AUTOCOMPLETETEXTVIEW1); Adapter=new arrayadapter<string> (this, Android.        R.layout.simple_list_item_1);        Lv.setadapter (adapter);        Dbhelper=new DBHelper (this);        Dbwrite=dbhelper.getwritabledatabase ();                GetData (); Click Submit Submit.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generate                D method stubstring Newdata=et.gettext (). toString (); AddData (NewData);}); Click on Sync upload.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generat        Ed method Stubgetop ();}}); Long press the list item, modify the data Lv.setonitemlongclicklistener (new Onitemlongclicklistener () {@Overridepublic Boolean Onitemlongclick (adapterview<?> arg0, View arg1,final int position, long Arg3) {//TODO auto-generated Metho D stubnew Alertdialog.builder (mainactivity.this). Setpositivebutton ("I'm sure to modify", new Dialoginterface.onclicklistener () { @Overridepublic void OnClick (dialoginterface dialog, int which) {//TODO auto-generated method Stubupdatedata (position);}        }). Show (); return false;});  Short press the list item to delete the data Lv.setonitemclicklistener (new Onitemclicklistener () {public void Onitemclick (Adapterview<?> arg0, View arg1, int position,long arg3) {//TODO auto-generated method Stubtoast.maketext (Mainactivity.this, position+ "", 0). s    How ();d eletedata (position);}}); }//Add Data Execution function, add to local database, and prompt update Listtview void addData (String newdata) {//Add to local database Contentvalues cont    Ent=new contentvalues ();        String Putdata=et.gettext (). toString ();        Content.put ("Data", putdata);        Dbwrite.insert ("DataTable", null, content);       The following saves the operation to the Operation table GetData (); C.movetolast ();        int Id=c.getint (C.getcolumnindex ("_id"));        Contentvalues content2=new contentvalues ();        Content2.put ("Data", putdata);        Content2.put ("Op", 0);        Content2.put ("Dataid", id);        Long Time=system.currenttimemillis ();        SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");        Date Date=new date (time);        String Formattime=sdf.format (date);        Content2.put ("timestamp", formattime);    Dbwrite.insert ("operation", NULL, CONTENT2); }//delete a data void deleteData (int position) {c.movetoposition (position); int Id=c.getint (C.getcolumnindex ("_i D "));d Bwrite.delete (" DataTable "," _id= ", New String[]{id+" "}); GetData ();        Record to Operation table Contentvalues content2=new contentvalues ();        Content2.put ("Data", "");        Content2.put ("Op", 1);        Content2.put ("Dataid", id);        Long Time=system.currenttimemillis ();        SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss"); DAte date=new date (time);        String Formattime=sdf.format (date);        Content2.put ("timestamp", formattime);    Dbwrite.insert ("operation", NULL, CONTENT2); }//Modify a data void UpdateData (int position) {c.movetoposition (position); Contentvalues cv=new contentvalues () cv.put ("Data", "changed"); int id=c.getint (C.getcolumnindex ("_id"));        Dbwrite.update ("DataTable", CV, "_id=?", new String [] {id+ ""});                GetData ();        Contentvalues content2=new contentvalues ();        Content2.put ("Data", "changed");        Content2.put ("Op", 2);        Content2.put ("Dataid", id);        Long Time=system.currenttimemillis ();        SimpleDateFormat sdf=new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss");        Date Date=new date (time);        String Formattime=sdf.format (date);        Content2.put ("timestamp", formattime);    Dbwrite.insert ("operation", NULL, CONTENT2); }//Get data void GetData () {c= dbwrite.query ("DataTable", NULL, NULL, NULL, NULL, NULL, NULL);     Adapter.clear ();        Reads the local database while (C.movetonext ()) {String data=c.getstring (c.getcolumnindex ("Data"));        int Id=c.getint (C.getcolumnindex ("_id"));        Adapter.add (data+ ":" +id);        System.out.println (data+ ":" +id);    } adapter.notifydatasetchanged ();     }//Get operation record from log table void Getop () {c= dbwrite.query ("operation", NULL, NULL, NULL, NULL, NULL, NULL);         while (C.movetonext ()) {String data=c.getstring (c.getcolumnindex ("Data"));         int Id=c.getint (C.getcolumnindex ("Dataid"));         int Op=c.getint (C.getcolumnindex ("Op"));         String timestamp=c.getstring (C.getcolumnindex ("timestamp"));         Toast.maketext (Mainactivity.this, id+ ":" +op+ "" +timestamp, 0). Show ();         System.out.println (id+ ":" +op+ "" +timestamp ");    } dbwrite.delete ("Operation", "1", null); }}

Dbhelper.java


package com.example.sqllitcache;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.sqliteopenhelper;public class DBHelper extends Sqliteopenhelper {public DBHelper (Context Context) {Super (context, "mydb", NULL, 1);//TODO auto-generated constructor stub} @Overridepublic void OnCreate ( Sqlitedatabase db) {//TODO auto-generated method Stubdb.execsql ("CREATE TABLE DataTable (" + "_id INTEGER Primar Y KEY AutoIncrement, "+" data String DEFAULT \ "\"), or//create an action table as a log, dataid for data item id,opt=0,1,2,3 to change the data and delete the Db.execsql ( "CREATE TABLE operation (" + "_id integer PRIMARY KEY autoincrement," + "Dataid Integer," + "Op intege R, "+" data string DEFAULT \ "\", "+" timestamp String "+");} @Overridepublic void Onupgrade (sqlitedatabase arg0, int arg1, int arg2) {//TODO auto-generated Method stub}} 


Android Data Cloud Sync (i) Generate Operation SQLite Record

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.