Android Note 3 -- SQLite database and dialog box, Android 3 -- sqlite
Now we will introduce how Android operates databases and several common android dialogs. Old Rules: Use a picture to introduce today's content.
If you cannot see the image clearly, right-click it and open it in a new window.
I. SQLite Database
SQLite is a lightweight database and an associated database management system complying with ACID (atomicity, consistency, isolation, and durability). It is mostly used in embedded development.
The SQLite database is non-typed. You 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 help class for database operations
Create a PersonOpenHelper class and rewrite the onCreate and onUpgrade Methods Following the SQLiteOpenHelper pumping class.
Public class PersonSQLiteOpenHelper extends SQLiteOpenHelper {/*** write the known fixed value (Database Name, version number ), only one Context is passed out to initialize the help class * @ param context */public PersonSQLiteOpenHelper (Context context) {// The constructor that calls four parameters this (context, "jin8.db", null, 3);}/*** create a database help class to create \ open \ manage database. * @ param context. * @ param name sets the database name * @ param factory CursorFactory defines a result set, cursor factory. * Cursor (result set, which saves the reference to the database, pointer) * if it is set to null, the default Cursor factory * @ param version indicates that the database version is used, starting from 1> = 1 */public PersonSQLiteOpenHelper (Context context, String name, CursorFactory factory, int version) {super (context, name, factory, version );} /*** called when the database is created for the first time. Create and initialize the table structure here. * // @ Overridepublic void onCreate (SQLiteDatabase db) {System. out. println ("PersonSQLiteOpenHelper: onCreate"); // execute the create statement and create the user table db.exe cSQL ("create table person (_ id integer primary key autoincrement, name varchar (20), age integer) ");}/*** called during Database Upgrade. Here, you can perform table structure update, deletion, and other operations. * // @ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {System. out. println ("PersonSQLiteOpenHelper: onUpgrade" + "Old Version:" + oldVersion + "New Version:" + newVersion); if (oldVersion = 2 & newVersion = 3) {// The version is upgraded from 2 to 3, and a balanceremainder field db.exe cSQL ("alter table person add column balance integer") is provided to the personaltable ");}}}
2. SQL statement
Public class PersonDAO2 {private PersonSQLiteOpenHelper helper; public PersonDAO2 (Context context) {helper = new PersonSQLiteOpenHelper (context);}/*** insert a piece of data, specify the name and age * @ param name * @ param age */public void insert (String name, int age) {// obtain the writable database SQLiteDatabase db = helper.getwritabledatabase(mongomongodb.exe cSQL ("insert into person (name, age) values (?, ?) ", New Object [] {name, age});}/*** delete a piece of data based on the specified name * @ param name */public void delete (String name) {SQLiteDatabase db = helper.getwritabledatabase(mongomongodb.exe cSQL ("delete from person where name =? ", New Object [] {name});}/*** modify a piece of data, modify the age of the specified person * @ param name * @ param age */public void update (String name, int age) {SQLiteDatabase db = helper.getwritabledatabase();mongodb.exe cSQL ("update person set age =? Where name =? ", New Object [] {age, name});}/*** query a piece of data * @ param name */public void querySingleRecord (String nameArg) {SQLiteDatabase db = helper. getReadableDatabase (); // ------------------------------------------------- key Cursor // cursor, reference to the database Cursor cursor = db. rawQuery ("select * from person where name =? ", New String [] {nameArg}); if (cursor! = Null & cursor. moveToFirst () {// whether it can be moved 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 the application cursor. close (); // --------------------------------------------------- the preceding response}/*** query all data */public List <Person> queryAll () {SQLiteDatabase db = helper. getReadableDatabase (); Cursor cursor = db. ra WQuery ("select * from person", null); ArrayList <Person> persons = new ArrayList <Person> (); if (cursor! = Null & cursor. getCount ()> 0) {// looping // obtain the number of columns int columnCount = cursor. getColumnCount (); System. out. println ("columnCount:" + columnCount); while (cursor. moveToNext () {// returns false if no data is found next time. getColumnIndex ("_ id") // retrieves the index int _ id = cursor Based on the column name. 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 (); persons. add (person) ;}} cursor. close (); return persons ;}}
2. Dialog Box 1: Notification dialog box
Public void showpolicydialog (View view) {// 1. notification dialog box Builder = new AlertDialog. builder (this); // set the icon builder. setIcon (android. r. drawable. ic_dialog_alert); // sets the title builder. setTitle ("reminder:"); // set the reminder content builder. setMessage ("Currently mobile network data, it is recommended to watch under wifi, whether to continue (local tyrants at Will)"); builder. setPositiveButton ("OK", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {Toast. makeText (MainActivity. this, "OK", 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 the return key can be used to disable it // create AlertDialog // AlertDialog dialog = builder. create (); // dialog. show (); // directly show (); builder. show ();}
2. display the 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 OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {System. out. println ("which:" + which) ;}}); builder. show ();}
3. The single answer dialog box is displayed.
Public void showSingleDialog (View view) {Builder builder = new AlertDialog. builder (this); builder. setTitle ("select gender:"); final String [] items = new String [] {"male", "female", "neutral", "Male Female ", "Female Male in the Past"}; builder. setSingleChoiceItems (items,-1, new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {System. out. println ("which:" + which) ;}}); builder. setPositiveButton ("OK", null); builder. show ();}
4. The check box is displayed.
Public void showMultiDialog (View view) {Builder builder = new AlertDialog. builder (this); builder. setTitle ("select interests:"); final String [] items = new String [] {"Smoking", "drinking", "hot stamping", "programming ", "niu"}; 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 ("OK", null); builder. show ();}
5. progress bar
Public void showProgressDialog (View view) {// display a loaded dialog box // ProgressDialog. show (this, "prompt:", "loading... please wait... "); final ProgressDialog progreprogre= new progressDialog (this); ProgressDialog. setTitle ("prompt:"); progressDialog. setMessage ("loading... "); // set the progressDialog style to horizontal. setProgressStyle (ProgressDialog. STYLE_HORIZONTAL); progressDialog. setMax (100); // when displayed, the progress is set to progressDialog. show (); new Thread () {public void run () {while (true) {SystemClock. sleep (50); // progressDialog. setProgress (I); progressDialog. incrementProgressBy (1); if (progressDialog. getProgress ()> = progressDialog. getMax () {progressDialog. dismiss (); break ;}}};}. start ();}