2. Android database SQLite sqliteopenhelper

Source: Internet
Author: User

/*************************************** *************

Everyone knows that it will be very tiring to write a blog.

Hope = reprint please indicate the source: http://blog.csdn.net/ta893115871

Please do not pity your mouse, (* ^__ ^ *) Xi ......

**************************************** ***********/

The source code has been shared in the resource.

Http://download.csdn.net/detail/ta893115871/4440305

 

The interface is as follows:

There is a problem: Why can't the menu icon be set? Now the SDK version 4.0.4 is used, and sdk2.1 is used previously.

First, the property Android: icon = "@ drawable/Add" cannot be displayed in iem in the. xml file.

The second is my setion (R. drawable. Add) in the Java file; No.

Which of the following heroes knows? Thank you very much for leaving me some comments.

 

Sqliteopenhelperdemo. Java

Package COM. example. sqliteopenhelperdemo; import android. OS. bundle; import android. app. activity; import android. database. cursor; import android. database. SQLite. sqlitecursor; import android. view. keyevent; import android. view. menu; import android. view. menuitem; import android. view. view; import android. widget. adapterview; import android. widget. adapterview. onitemclicklistener; import android. widget. adapterview. on Itemselectedlistener; import android. widget. edittext; import android. widget. listview; import android. support. v4.widget. simplecursoradapter; public class sqliteopenhelperdemo extends activity {private edittext medittext; private listview mlistview; private simplecursoradapter adapter; private mydatabase database; private cursor mcursor; private int _ id; // The field value of the current cursor @ suppresswarnings ("deprecation") @ over Ridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_sqlite_open_helper_demo); medittext = (edittext) This. findviewbyid (R. id. edit_id); mlistview = (listview) This. findviewbyid (R. id. listview_id); mlistview. setonitemclicklistener (clicklistener); mlistview. setonitemselectedlistener (selectedlistener); // create a reference database for the sqliteopenhelper object = new Mydatabase (this);/* obtain the information in the database */mcursor = database. query ();/* New simplecursoradapter and input mycursor. The data field is displayed as _ id, todo_text */adapter = new simplecursoradapter (this, R. layout. list, mcursor, new string [] {mydatabase. field_id, mydatabase. field_text}, new int [] {R. id. itemid_id, R. id. itemcon_id,}); mlistview. setadapter (adapter) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {getmenuinflater (). infl Ate (R. menu. activity_sqlite_open_helper_demo, menu); Return true;} public Boolean onmenuitemselected (INT featureid, menuitem item) {Switch (item. getitemid () {case R. id. menu_add: This. add (); break; case R. id. menu_modfi: This. moD (); break; case R. id. menu_delete: This. delete (); break; default: break;} return false;}; // listview Click Event Processing onitemclicklistener clicklistener = new onitemclicklistener () {public void oni Temclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {// todo auto-generated method stubmcursor. movetoposition (arg2); _ id = mcursor. getint (mcursor. getcolumnindex (mydatabase. field_id); medittext. settext (mcursor. getstring (mcursor. getcolumnindex (mydatabase. field_text) ;}}; // listen to the event handling the mouse wheel processing onitemselectedlistener selectedlistener = new onitemselectedlistener () {public void onitemselected (adapterview <?> Arg0, view arg1, int arg2, long arg3) {// todo auto-generated method stub_id = mcursor. getint (mcursor. getcolumnindex (mydatabase. field_id); sqlitecursor c = (sqlitecursor) arg0.getselecteditem (); medittext. settext (C. getstring (C. getcolumnindex (mydatabase. field_text);} public void onnothingselected (adapterview <?> Arg0) {// todo auto-generated method stub}; // update listviewpublic void myupdateshow () {/* re-query */mcursor. requery (); // inform the adapter that the adapter has changed. notifydatasetchanged (); medittext. settext (""); _ id = 0;} // Add public void add () {string STR = medittext. gettext (). tostring (); If (Str. equalsignorecase ("") {return;}/* add data to the database */database. addmethod (STR); myupdateshow () ;}// modify public void Mod () {string STR = medittext. gettext (). tostring (); If (Str. equalsignorecase ("") {return;} // modify the database. modmethod (_ id, STR); myupdateshow () ;}// Delete public void Delete () {If (_ id = 0) {return;} database. deletemethod (_ id); myupdateshow () ;}@ overridepublic Boolean onkeydown (INT keycode, keyevent event) {// todo auto-generated method stubif (keycode = keyevent. keycode_back) {// closes the database and exits the program database. close (); this. finish ();} return Super. onkeydown (keycode, event );}}

Mydatabase. Java

Package COM. example. sqliteopenhelperdemo; import android. content. contentvalues; import android. content. context; import android. database. cursor; import android. database. SQLite. sqlitedatabase; import android. database. SQLite. sqliteopenhelper; import android. util. log; public class mydatabase extends sqliteopenhelper {Private Static final string database_name = "demo_db"; // database name Private Static final int databa Seversion = 1; // version Private Static final string table_name = "demo_table"; // The table name public static final string field_id = "_ id "; // It Must Be _ idpublic static final string field_text = "demo_text"; private sqlitedatabase dB; // database Private Static final string tag = "mydatabase"; Public mydatabase (context) {super (context, database_name, null, databaseversion); // todo auto-generated constructor stubdb = This. get Writabledatabase (); // open or create a database (created at the first time) to obtain the sqlitedatabase object. To read and write data} @ overridepublic void oncreate (sqlitedatabase dB) {// todo auto-generated method stub/* Create Table */log. I (TAG, "oncreate ()"); string SQL = "CREATE TABLE" + table_name + "(" + field_id + "integer primary key autoincrement, "+" "+ field_text +" text) "mongodb.exe csql (SQL);} // update Database @ overridepublic void onupgrade (sqlitedatabase dB, int ol Dversion, int newversion) {// todo auto-generated method stublog. I (TAG, "onupgrade ()"); // Delete the sqlstring SQL = "Drop table if exits" + table_namemongodb.exe csql (SQL); oncreate (db );} // close the Database @ overridepublic synchronized void close () {// todo auto-generated method stublog. I (TAG, "close ()"); dB. close (); super. close () ;}@ overridepublic void onopen (sqlitedatabase db1) {// todo auto-generated method stublog. I (Tag, "onopen ()"); super. onopen (db1);} // Add public long addmethod (string Str) {/* Add the new value to contentvalues */contentvalues CV = new contentvalues (); cv. put (field_text, STR); // key-Value Pair long row = dB. insert (table_name, null, CV); log. I (TAG, "addmethod ROW =" + row); Return row;} // Delete public void deletemethod (INT ID) {string [] whereargs = {integer. tostring (ID)}; int rowsaffected = dB. delete (table_name, field_id + "=? ", Whereargs); log. I (TAG, "deletemethod () rowsaffected =" + rowsaffected);} // modify public void modmethod (INT ID, string Str) {contentvalues values = new contentvalues (); values. put (field_text, STR); string [] whereargs = {integer. tostring (ID)}; int rowsaffected = dB. update (table_name, values, field_id + "=? ", Whereargs); log. I (TAG, "modmethod () rowsaffected =" + rowsaffected);} // query all data and return the cursor object public cursor query () {// ASC is in ascending order. DESC is in descending order (ASC by default). Return dB. query (table_name, null, field_id + "ASC ");}}

No more
For other layout XML files, see Resource:

Http://download.csdn.net/detail/ta893115871/4440305

 

Related Article

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.