Now let's talk about how Android operates the database and several common android dialog boxes. The usual, use a picture to introduce today's content.
If you can't see the picture, you can right-click the new window.
First, SQLite database
SQLite, a lightweight database, is an associative database management system that adheres to ACID (atomicity, consistency, isolation, durability) and is used in embedded development.
The SQLite database is untyped and can add a string to an integer column, but it also supports common types such as: NULL, VARCHAR, TEXT, Integer, BLOB, CLOB, etc.
1, create a database helper class for manipulating the database
Create a Personopenhelper class that inherits the Sqliteopenhelper abstract class, overriding the OnCreate and Onupgrade methods.
public class Personsqliteopenhelper extends Sqliteopenhelper {/** * Write a known fixed value (database name, version number), outside only a context comes in to initialize the helper class * @ Param context */public Personsqliteopenhelper (context context) {//Transpose 4 parameters to the constructor of this (context, "jin8.db", NULL, 3);} /** * Create a database helper class to create \ Open \ Manage Database. * @param context contexts. * @param Name Set database name * @param factory cursorfactory Defines a result set, a cursor factory. * CURSOR cursor (result set, save reference to database, pointer) * Set to NULL to use system default cursor factory * @param version database, starting from 1 >= 1 */public personsqliteopenhe Lper (context context, String name,cursorfactory factory, int version) {Super (context, name, Factory, version);} /** * Called when the database is created for the first time, the table structure is created and initialized. */@Overridepublic void OnCreate (Sqlitedatabase db) {System.out.println ("personsqliteopenhelper:oncreate");//Execute CREATE statement , create the person table Db.execsql ("CREATE TABLE person (_id Integer primary key autoincrement, name varchar (), age integer)"); /** * Database Upgrade when called, here do table structure Update, delete and other operations. */@Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {System.out.println ("PErsonsqliteopenhelper:onupgrade "+" old version number: "+ oldversion +" new version number: "+ newversion); if (oldversion = = 2 && newvers Ion = = 3) {//version upgraded from 2 to 3, add a Balance Balance field db.execsql ("ALTER TABLE person add column Balance integer") to the Person table;}}
2,sql Action Statements
public class PersonDAO2 {private Personsqliteopenhelper helper;public PersonDAO2 (context context) {helper = new Personsqliteopenhelper (context);} /** * Inserts a data, specify name and age * @param name * @param */public void Insert (String name, int ages) {//Get writable database Sqlitedatabase db = Helper.getwritabledatabase ();d b.execsql ("INSERT into person (name, age) VALUES (?,?)", New Object[]{name, age}); /** * Deletes a data according to the specified name * @param name */public void Delete (String name) {Sqlitedatabase db = Helper.getwritabledatabase ();d B.ex Ecsql ("Delete from person where name =?", new Object[]{name});} /** * Modify a data to modify the age of the specified person * @param name * @param */public void Update (String name, int ages) {Sqlitedatabase db = Helper.g Etwritabledatabase ();d b.execsql ("update person set age =?") WHERE name =? ", new Object[]{age, name});} /** * Query a data * @param name */public void Querysinglerecord (String namearg) {Sqlitedatabase db = Helper.getreadabledatabase ();//-------------------------------------------------Focus ↓//cursor, reference to database cursor cursor = Db.rawquery ("select * from person where name =?", new String[]{namearg}); if (cursor! = NULL && Cursor.movetofir St ()) {//can move to the first int _id = cursor.getint (0); String name = cursor.getstring (1); int age = Cursor.getint (2); System.out.println ("_id:" + _id + "Name:" + name + "Age:" + age); Close Application Cursor.close ();//-------------------------------------------------above ↑}/** * Query all data */public List<person > Queryall () {Sqlitedatabase db = Helper.getreadabledatabase (); cursor cursor = db.rawquery ("SELECT * from person", NULL); arraylist<person> persons = new arraylist<person> (); if (cursor! = NULL && cursor.getcount () > 0) {/ /loop traversal//Get the number of columns int columnCount = Cursor.getcolumncount (); System.out.println ("ColumnCount:" + ColumnCount), while (Cursor.movetonext ()) {//until next no data, return false// Cursor.getcolumnindex ("_id")//Gets the index of the column according to the column name int _id = Cursor.getint (Cursor.getcolumnindex ("_id")); String name = cursor.getstring (Cursor.getcolumnindex ("name")); int age = Cursor.getint (cuRsor.getcolumnindex ("Age")); person person = new person (_id, name, age); System.out.println (Person.tostring ());p ersons.add (person);}} Cursor.close (); Return persons;}}
Ii. dialog box 1, Notification dialog box
public void Shownotifydialog (view view) {//1. notification Dialog Builder Builder = new Alertdialog.builder (this);// Set icon Builder.seticon (Android. R.drawable.ic_dialog_alert)///Set title Builder.settitle ("Reminder:");//Set Reminder content Builder.setmessage ("Currently mobile network data, recommended to watch under WiFi, Whether to continue (local tyrants) "); Builder.setpositivebutton (" Confirm ", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Toast.maketext (Mainactivity.this, "confirm", 0). Show ();}); Builder.setnegativebutton ("Cancel", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Toast.maketext (Mainactivity.this, "Cancel", 0). Show ();}); Builder.setcancelable (FALSE); Whether you can close//create Alertdialog//alertdialog Dialog = Builder.create () by the return key,//dialog.show ();//Direct show (); Builder.show ();}
2, Display list dialog box
public void Showlistdialog (view view) {Builder builder = new Alertdialog.builder (this); Builder.settitle ("select language"); string[] STRs = new string[]{"Java", "C + +", "C", "PHP", "C #", "C", "PHP", "C #"};builder.setitems (STRs, New Onclicklisten ER () {@Overridepublic void OnClick (dialoginterface dialog, int which) {System.out.println ("which:" + which);}); Builder.show ();}
3, display a radio dialog box
public void Showsingledialog (view view) {Builder builder = new Alertdialog.builder (this); Builder.settitle ("selectivity:"); Final string[] items = new string[]{"Male", "female", "neutral", "former male female", "formerly Female Male"};builder.setsinglechoiceitems (items, 1, new ONCLI Cklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {System.out.println ("which:" + which);}); Builder.setpositivebutton ("confirm", null); Builder.show ();}
4, Display the Check dialog box
public void Showmultidialog (view view) {Builder builder = new Alertdialog.builder (this); Builder.settitle ("Choose hobby:"); Final string[] items = new string[]{"Smoking", "drinking", "Perm", "Programming", "Courting"};final boolean[] checkeditems = new Boolean[]{true, False , True, False, False};builder.setmultichoiceitems (Items,checkeditems, New Onmultichoiceclicklistener () {@ overridepublic void OnClick (dialoginterface dialog, int which, Boolean isChecked) {System.out.println ("which:" + which + "isChecked:" + isChecked); Checkeditems[which] = isChecked;}}); Builder.setpositivebutton ("confirm", null); Builder.show ();}
5, progress bar
public void Showprogressdialog (view view) {//Displays a loaded dialog box//progressdialog.show (this, "Hint:", "Loading in, please later ..."); final ProgressDialog ProgressDialog = new ProgressDialog (this);p rogressdialog.settitle ("Hint:");p Rogressdialog.setmessage ( "Loading ...");//Set the style to landscape Progressdialog.setprogressstyle (progressdialog.style_horizontal);p Rogressdialog.setmax ( 100);//Display the progress to zero progressdialog.show (); new Thread () {public void run () {while (true) {systemclock.sleep (50);// Progressdialog.setprogress (i);p Rogressdialog.incrementprogressby (1); if (progressdialog.getprogress () >= Progressdialog.getmax ()) {Progressdialog.dismiss (); break;}}};}. Start ();}
Android Note 3--sqlite Database and dialog box