/** * A particular {@link Asyncqueryhandler} allowing clients to be notified via A * listener. The {@link Notifyingasyncqueryhandler} also make sure no strong * Reference are kept on the given listener (as it is often A Context). * */public class Dbasynchandler extends Asyncqueryhandler {private weakreference<dbasynclistener> mListener;/** * Listen to completed query operations. Run on UI * Thread */public Static class Dbasynclistener {public Dbasynclistener () {}/** * run on UI Thread <br/> * <b>cursor Close () method Needn ' t be called</b> */protected void onquerycomplete (int token, Object cookie, Cur SOR cursor) {}protected void oninsertcomplete (int token, Object cookie, Uri uri) {}protected void onupdatecomplete (int tok En, object cookie, int result) {}protected void ondeletecomplete (int token, object cookie, int result) {}}/**instance in U I thread*/public Dbasynchandler (contentresolver resolver, Dbasynclistener Listener) {super (resolver);Setdblistener (listener);} /** * Assign The given {@link Dbasynclistener}. */public void Setdblistener (Dbasynclistener listener) {Mlistener = (listener! = null)? New Weakreference<dbasyncliste Ner> (listener): null;} Private Dbasynclistener Getdblistener () {return (Mlistener = = null)? Null:mListener.get (); /** such as Activity-ondestory (), this method is called */public void Cleardblistener () {mlistener = null;} public void Startquery (int token, Object cookie, Uri Uri, string[] projection, String selection, string[] Selectionargs, S Tring) {super.startquery (token, cookie, URI, projection, selection, Selectionargs, by-and-by);} @Overrideprotected void Onquerycomplete (int token, Object cookie, cursor cursor) {Dbasynclistener listener = Getdblistene R (); if (listener! = null) {Listener.onquerycomplete (token, cookie, cursor); if (cursor! = NULL &&!cursor.isclosed ()) cursor.close (); else if (cursor! = NULL) {Cursor.close ();}} @Overrideprotected void Ondeletecomplete (int token, Object cookie, int result) {Dbasynclistener listener = Getdblistener (); if (listener! = null) {Listener.ondeletecomplete (t Oken, cookie, result);}} @Overrideprotected void Oninsertcomplete (int token, Object cookie, Uri uri) {Dbasynclistener listener = Getdblistener (); F (listener! = null) {Listener.oninsertcomplete (token, cookie, URI);}} @Overrideprotected void Onupdatecomplete (int token, Object cookie, int result) {Dbasynclistener listener = Getdblistener ( if (listener! = null) {Listener.onupdatecomplete (token, cookie, result);}}}
public class MyProvider extends ContentProvider {private final static String TAG = MyProvider.class.getSimpleName ();p Riva Te final static int person = 1;public final static String BASE = "Com.baidu.my.provider";p ublic final static Uri Uri_perso n = uri.parse ("Content://com.baidu.my.provider/person");p rivate final static Urimatcher MATCHER = new Urimatcher ( Urimatcher.no_match); If the match is unsuccessful, the parameter value specified in the constructor is returned, the default is -1private sqlitedatabase db;static {matcher.adduri (BASE, person);} @Overridepublic Boolean onCreate () {Mydbhelper dbhelper = new Mydbhelper (This.getcontext ());d B = Dbhelper.getwritabledatabase (); return true;} @Overridepublic Cursor query (Uri uri, string[] projection, string selection, string[] Selectionargs, string sortOrder) {Cu Rsor C = Null;switch (Matcher.match (URI)) {Case person:c = db.query (dbperson.tablename, projection, selection, Selectiona RGS, NULL, NULL, SortOrder), C.setnotificationuri (GetContext (). Getcontentresolver (), Uri_person); Break;default: break;} return c;} @OverRidepublic String getType (Uri uri) {switch (Matcher.match (URI)) {case Person:return "vnd.android.cursor.dir/vnd." + BASE + ". Request";d Efault:break;} return null;} @Overridepublic uri insert (URI uri, contentvalues values) {Long id = 0;switch (matcher.match (URI)) {Case person:id = db.in SERT (dbperson.tablename, NULL, values), if (Id > 0) {//data changed, Notification GetContext (). Getcontentresolver (). Notifychange ( Uri_person, null);} Return Contenturis.withappendedid (URI, id);} return null;} @Overridepublic int Delete (URI Uri, String selection, string[] Selectionargs) {//TODO auto-generated method Stubreturn 0; } @Overridepublic int update (URI uri, contentvalues values, String selection, string[] Selectionargs) {//TODO Auto-generat Ed method Stubreturn 0;}}
public class Mydbhelper extends Sqliteopenhelper {private static final String TAG = "Dbhelp ER ";p rivate static final String db_name =" my.db ";p rivate static final int db_version =/**/57/**/;/**** Carefully modified, otherwise it will result in user history data Deleted ****/public Mydbhelper (context context) {Super (context, db_name, NULL, db_version);} @Overridepublic void OnCreate (Sqlitedatabase db) {Db.execsql (Getrequestcreatestr ());} @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {db.execsql ("DROP TABLE IF EXISTS" + db Person.tablename); onCreate (db);} Private String Getrequestcreatestr () {stringbuffer sb = new StringBuffer (); Sb.append ("CREATE TABLE"). Append ( Dbperson.tablename) Sb.append ("("); Sb.append (basecolumns._id). Append ("INTEGER PRIMARY KEY,"); Sb.append ( DBPerson.MetaDate.Name). Append ("text,"); Sb.append (DBPerson.MetaDate.Age). Append ("text)"); return sb.tostring ();}}
public interface Dbperson extends Basedb{public interface Metadate {String name = "Name"; String age = "Age";} public string TableName = ' person ';p ublic string[] PROJECTION = {metadate.name, metadate.age};p ublic String sort_order = Metadate.age + "ASC";}
public class Mainactivity extends Activity {private Dbasynchandler dbasynchandler; @Overrideprotected void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// Dbasynchandler = new Dbasynchandler (Getcontentresolver (), Dblistener);} Private Dbasynchandler.dbasynclistener Dblistener = new Dbasynclistener () {protected void Onquerycomplete (int token, Object cookie, cursor cursor) {Toast.maketext (Getapplicationcontext (), "OK =" + Cursor.getcount (), 1). Show (); protected void Oninsertcomplete (int token, Object cookie, Android.net.Uri Uri) {Toast.maketext (Getapplicationcontext () , "Insert ok! ", 1). Show ();}}; public void query (view view) {//querydbasynchandler.startquery, NULL, Myprovider.uri_person, Dbperson.projection, NULL, NULL, Dbperson.sort_order);} public void Insert (view view) {//insertcontentvalues initialvalues = new Contentvalues (); Initialvalues.put ( DBPerson.MetaDate.Name, "Baidu"); Initialvalues.put (DBPerson.MetaDate.Age, System.Currenttimemillis ());d Basynchandler.startinsert (+, NULL, Myprovider.uri_person, initialvalues);} @Overrideprotected void OnDestroy () {//cleardbasynchandler.cleardblistener (); Super.ondestroy ();}}
SOURCE Download ""
Android_ Database _ Asynchronous Operation encapsulation