Find and change the SQLite data sheet in Android

Source: Internet
Author: User
Tags gettext

Directly on the code

Mainactiviy.java


Package Com.example.sqllitcache;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.listview;import Android.widget.toast;public class Mainactivity extends Activity {ListView lv; Button submit; EditText et; Sqlitedatabase dbwrite;D bhelper dbhelper; Arrayadapter <String> Adapter;    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);        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);}); Long press list item, modify data Lv.setonitemlongclicklistener (new Onitemlongclicklistener () {@Overridepublic Boolean onitemlongclick (A Dapterview<?> arg0, View arg1,final int position, long Arg3) {//TODO auto-generated method Stubnew Alertdialog.buil Der (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 content=n    EW contentvalues ();        String Putdata=et.gettext (). toString ();        Content.put ("Data", putdata);        Dbwrite.insert ("DataTable", null, content);    GetData (); }//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 (); }//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"));d bwrite.update ("DataTable", CV, "_id=?", new String [] {id+ ""});    GetData ();    }//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 (); }}


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 PRIMARY KEY autoincrement," +           "data String DEFAULT \" \ ")"  );} @Overridepublic void Onupgrade (sqlitedatabase arg0, int arg1, int arg2) {//TODO auto-generated method stub}}


Find and change the SQLite data sheet in Android

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.