SQLite database
// Custom class myopenhelper inherit from Sqliteopenhelper New null, 1); // Obtain the database object, if the database does not exist, first create the database, then obtain it, if it exists, directly Sqlitedatabase db = Oh.getwritabledatabase ();
Getwritabledatabase (): Open the database corresponding Sqlitedatabase object in read and write mode, error when disk space is insufficient
Getreadabledatabase (): The function first calls Getwritabledatabase (), if there is insufficient disk space, returns an open failure, and then opens the corresponding Sqlitedatabase object for the database in a read-only manner
Create a table when you create a database
Public void onCreate (Sqlitedatabase db) { // TODO auto-generated method stub db.execsql (" CREATE TABLE person (_id Integer primary key autoincrement, name Char (TEN), Phone char (), Money Integer) ");
Database additions and deletions change
SQL statements
Insert intoPerson (name, phone, Money)Values('Zhang San','159874611', -); Delete fromPersonwhereName= 'John Doe' and_id= 4; UpdatePersonSet Money = 6000 whereName= 'John Doe'; SelectName, phone fromPersonwhereName= 'Zhang San';
Execute SQL statements to implement additions and deletions
New object[]{"Zhang San", 15987461, 75000}); // Insert New string[]{"Zhang San"}); // Find
This method is called before the test method executes
protected void throws Exception { super. SetUp (); // get the virtual context object New null, 1);}
Use API to implement additions and deletions
Insert
// encapsulates the data to be inserted into a Contentvalues object in the form of a key-value pair New contentvalues (); Cv.put ("name", "Liu Can"), Cv.put ("Phone", 1651646); Cv.put ("Money", 3500 ); Long null, CV); // The return value is the primary key of the inserted row and returns 1 if an error occurs
Delete
int New string[]{"1", "Zhang San"}); // The return value is the number of rows deleted
Modify
New contentvalues (); Cv.put ("Money", 25000); int New string[]{"Zhao Si"}); // The return value is the primary key of the modified row and returns 1 if an error occurs
Find
//arg2: The field to query//arg3: Query Criteria//Arg4: Placeholder for populating query criteriaCursor cs = db.query ("Person",Newstring[]{"Name", "Money"}, "name =?",Newstring[]{"Zhang San"},NULL,NULL,NULL); while(Cs.movetonext ()) {//gets the index value of the specified columnString name = cs.getstring (Cs.getcolumnindex ("name")); String Money= Cs.getstring (Cs.getcolumnindex ("Money")); SYSTEM.OUT.PRINTLN (Name+ ";" +Money );}
Transaction
Ensure that multiple SQL statements either succeed at the same time or fail at the same time
Most common case: bank transfer
Transaction API
Try { // open transaction db.begintransaction (); ........... // SET Transaction Execution Success finally { // Close transaction / / If the transaction execution succeeds at this time, the SQL statement takes effect, otherwise it does not take effect db.endtransaction ();}
Display data from the database to the screen
1. Arbitrarily inserting some data
2. Define Business Bean:Person.java
3. Read all data from the database
Cursor cs = db.query ("Person",NULL,NULL,NULL,NULL,NULL,NULL); while(Cs.movetonext ()) {String name= Cs.getstring (Cs.getcolumnindex ("name")); String Phone= Cs.getstring (Cs.getcolumnindex ("Phone")); String Money= Cs.getstring (Cs.getcolumnindex ("Money")); //encapsulates the read data into a person objectPerson p =NewPerson (name, phone, money); //Save the Person object to the collectionPeople.add (P);}
4. Display the data in the collection to the screen
LinearLayout ll =for(person p:people) { // create TextView, each piece of data is displayed with a text box New TextView (this); Tv.settext (P.tostring ()); // Set the text box to the child node of LL Ll.addview (TV);}
Paging Query
NULL NULL NULL NULL NULL null, "0, 10");
Listview
Lsitview used to display a line of items
MVC architecture
M:model model layer, the data to be displayed ———— people collection
V:view view layer, the user sees the interface ———— ListView
C:control control layer, manipulating how data is displayed ———— adapter object
Each entry is a View object
Baseadapter
Two methods that must be implemented
First one
// The system calls this method to learn how many data the model layer has Public int GetCount () { return people.size ();}
A second
//The system calls this method to get the view object to display to the ListView//position: Is the position of the data in the collection for the Return view object PublicView GetView (intposition, View Convertview, ViewGroup parent) {System.out.println ("GetView method call" +position); TextView TV=NewTextView (mainactivity. This); //get the elements in the collectionPerson p =People.get (position); Tv.settext (P.tostring ()); //return the TextView object and it will become the entry for the ListView. returnTV;}
How many entries are displayed on the screen, how many times the GetView method will be called, and GetView will continue to be called when the screen is slid down, creating more view objects to display to the screen
Cache of Entries
When the entry is a screen, the system will cache the entry into memory, when the entry again into the screen, the system will re-call GetView when the cache entry as the Convertview parameter, but the incoming entry is not necessarily the previously cached entry, That is, it is possible for the system to pass the cache of any entry when it calls the GetView method to get the first entry
dialog box
OK Cancel dialog box
Create a Dialog builder object similar to Factory mode
New Builder (this);
Set the title and body
Builder.settitle ("Warning"); Builder.setmessage ("If you practice this work, you must first be self-palace");
Set OK and Cancel buttons
Builder.setpositivebutton ("Now self-palace",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { //TODO auto-generated Method StubToast.maketext (mainactivity. This, "Congratulations on your self-palace success, now program quit", 0). Show (); }}); Builder.setnegativebutton ("Next time,"NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) { //TODO auto-generated Method StubToast.maketext (mainactivity. This, "If not from the palace, must not succeed", 0). Show (); }});
Using the Builder to create a dialog box object
Alertdialog ad = builder.create (); Ad.show ();
Single-selection dialog box
New Builder (this); Builder.settitle ("Choose your Gender");
Define a radio option
Finalstring[] Items =Newstring[]{Male, Female};//-1 means no one is selected by default//Click to listen to the guide package to pay attention to the wrong guideBuilder.setsinglechoiceitems (items,-1,NewOnclicklistener () {//dialog: A dialog box that triggers this method//which: Subscript for user-selected entry@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Toast.maketext (mainactivity). This, "You have selected" + Items[which], 0). Show (); //Close the dialog boxDialog.dismiss (); }});//You can display the dialog box directly with the builderBuilder.show ();
Multi-Select dialog box
New Builder (this); Builder.settitle ("Please select the person you think is handsome");
The option to define multiple selections, because you can select multiple, so you need a Boolean array to record which options are selected
Finalstring[] Items =Newstring[]{"Daniel Wu", "Anna", "Andy Lau", "Louis Koo"};//only the first entry is selected by defaultFinal Boolean[] CheckedItems =New Boolean[]{ true, false, false, false,};builder.setmultichoiceitems (items, CheckedItems,NewOnmultichoiceclicklistener () {//which: the user clicks on the subscript of the entry//isChecked: Used to determine whether the user clicked the entry is selected or canceled@Override Public voidOnClick (Dialoginterface Dialog,intWhich,BooleanisChecked) {Checkeditems[which]=isChecked; }});//set a OK buttonBuilder.setpositivebutton ("OK",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {StringBuffer SB=NewStringBuffer (); for(inti = 0;i < items.length; i++) {sb.append (Checkeditems[i]? Items[i] + "": ""); } toast.maketext (mainactivity. This, sb.tostring (), 0). Show (); }}); Builder.show ();
Android app development-data storage and Interface display (ii) (re-plate)