Learning SQLite in Android

Source: Internet
Author: User

Added, deleted, and queried data. Bind the spinner control and the listview control to click the delete list option.

 

Package COM. example. splite; import Java. util. arraylist; import Java. util. list; import android. content. contentvalues; import android. content. context; import android. database. cursor; import android. database. SQLite. sqlitedatabase; import android. database. SQLite. sqlitedatabase. cursorfactory; import android. database. SQLite. sqliteopenhelper; import android. util. log; public class dbhelpe extends sqliteopenhelper {Private Static final int database_version = 1; Private Static final string database_name = "test"; Private Static final string table_labels = "labels "; private Static final string key_id = "ID"; Private Static final string key_name = "name"; sqlitedatabase dB; Public dbhelpe (context) {super (context, database_name, null, database_version); // todo auto-generated constructor stub} @ override public void oncreate (sqlitedatabase dB) {// todo auto-generated method stub string SQL = "CREATE TABLE" + table_labels + "(" + key_id + "integer primary key," + key_name + "text )"; db.exe csql (SQL) ;}@ override public void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// todo auto-generated method stub db.exe csql ("Drop table if exists" + table_labels); oncreate (db) ;}// add public void insertlabel (string label) {DB = This. getwritabledatabase (); contentvalues values = new contentvalues (); values. put (key_name, label); dB. insert (table_labels, null, values); dB. close (); // closing database connection} // search for public list <string> getalllabels () {list <string> labels = new arraylist <string> (); string selectquery = "select * from" + table_labels; sqlitedatabase DB = This. getreadabledatabase (); cursor = dB. rawquery (selectquery, null); If (cursor. movetofirst () {do {labels. add (cursor. getstring (1);} while (cursor. movetonext ();} cursor. close (); Return labels;} // Delete public void Delete (string name) {If (DB = NULL) dB = This. getreadabledatabase (); string SQL = "delete from labels where name = '" + name + "'"; db.exe csql (SQL );}}

There is a method in the main class, that is, when you click the add data button, the text input box of the system is automatically hidden to implement code

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);                imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);

 

Package COM. example. splite; import Java. util. list; import android. OS. bundle; import android. app. activity; import android. content. context; import android. database. cursor; import android. util. log; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. view. inputmethod. inputmethodmanager; import android. widget. adapterview; import android. widget. adapterview. oni Temclicklistener; import android. widget. arrayadapter; import android. widget. button; import android. widget. edittext; import android. widget. listview; import android. widget. scrollview; import android. widget. spinner; import android. widget. toast; public class mainactivity extends activity implements onitemclicklistener {spinner; edittext; button; scrollview SCR; listview list; cursor m Cursor; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); List = (listview) findviewbyid (R. id. listview); button = (button) findviewbyid (R. id. btn_add); edittext = (edittext) findviewbyid (R. id. input_label); spinner = (spinner) findviewbyid (R. id. spinner); list. setonitemclicklistener (this); button. setonclicklistener (n EW onclicklistener () {public void onclick (view v) {// todo auto-generated method stub string data = edittext. gettext (). tostring (). trim (); If (data. trim (). length ()> 0) {dbhelpe DB = new dbhelpe (getapplicationcontext (); dB. getwritabledatabase (); int A = 0; A = dB. getalllabels (). size (); dB. insertlabel (data); log. D ("DDD", DB. getalllabels (). size () + ""); If (dB. getalllabels (). size ()> A) {toast. maketext (geta Pplicationcontext (), "inserted successfully", toast. length_short ). show ();} else {toast. maketext (getapplicationcontext (), "insertion failed", toast. length_short ). show ();} log. D ("AA", A + ""); edittext. settext (""); inputmethodmanager Imm = (inputmethodmanager) getsystemservice (context. input_method_service); Imm. hidesoftinputfromwindow (edittext. getwindowtoken (), 0); loaddata ();} else {toast. maketext (getapplicationcontext (), "Enter Input data ", toast. length_short ). show () ;}}); loaddata () ;}public void loaddata () {dbhelpe DB = new dbhelpe (getapplicationcontext (); List <string> DATA = dB. getalllabels (); arrayadapter <string> adapter = new arrayadapter <string> (this, android. r. layout. simple_spinner_item, data); adapter. setdropdownviewresource (Android. r. layout. simple_spinner_dropdown_item); spinner. setadapter (adapter); arrayadapter <s Tring> listadapter = new arrayadapter <string> (this, android. r. layout. simple_list_item_1, data); list. setadapter (listadapter) ;}@ override public Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}@ override public void onitemclick (adapterview <?> Arg0, view arg1, int arg2, long arg3) {// todo auto-generated method stub dbhelpe DB = new dbhelpe (getapplicationcontext (); dB. getreadabledatabase (); string data = arg0.getitematposition (arg2 ). tostring (); log. D ("Sss", data); dB. delete (data); loaddata ();}}

 

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.