shared prefrences parameter sharing
Storage location: data/data/Package name/shared_prefs/mainacitivy.xml
Format: xml
Save data:
//Get shared Prefrences type Object
Sharedprefrences sp = getsharedprefrences ("xxx", 0);
or
Sharedprefrences sp = getprefrences (0);
//Get an object of type edit
Edit edit = Sp.edit ();
//Write Data
Edit.putstring ("name", "Xiaoming");
Edit.putboolean ("IsTrue", "true");
//Submit data ( must have )
Extract data
/Get Sharedprefrences type Object
Sharedprefrences sp = getsharedprefrences ("xxx", 0);
or
Sharedprefrences sp = getprefrences (0);
//Remove the corresponding data based on the key value stored
String name = sp.getstring ("name", Little Red);
Boolean B = Sp.getboolean ("IsTrue", false);
SQLite Database Storage
Android Platform Integration Small database SQLite
Features: cross-platform, free, lightweight, multi-threaded
SQL statements:
Related to table:
A. Build table: CREATE table table name (ID INTEGER PRIMARY KEY autoincrement,name varchar), phone varchar (20))
B. Deletion of tables: DROP table name
C. Change table: ALERT table name ADD age INTEGER
Related to data: increase, delete, change, check
A. Adding data: INSERT into table name (field name) values (value)
B. Deleting data: Delete from table name WHERE condition
C. Modifying data: UPDATE table name SET field name = value WHERE condition
D. Querying data: SELECT * FROM table name WHERE condition GROUP BY grouping order by sort
Steps to create a SQLite database:
A. Creating an auxiliary class to inherit Sqliteopenhelper
B. In the OnCreate () method of the auxiliary class, add the statement that creates the table and execute the statement
String str = "CREATE TABLE student (id integer PRIMARY KEY autoincrement,name varchar (), age INTEGER, phone VARCHAR (20))" ;
Db.execsql (str);
C. In activity, create a secondary class object, and then use the helper class object to call getreadabledatabase () or getwriteabledatabase () to create the database object
Note: When both the database and the table already exist, the OnCreate () method no longer calls the
When version changes, the Onupgrade () method is called
SQL statement instance:
(1) Increase
String str = "INSERT into student (Name,phone) VALUES (?,?)";
Mdb.execsql (str,new string[]{"Xiao Ming", "1111"});
(2) by deleting
String str = "DELETE from student WHERE name =?";
Mdb.execsql (str,new string[]{"Xiao Ming"});
(3) Change
String str = "UPDATE student SET name =?, phone =?" WHERE id =? ";
Mdb.execsql (str,new string[]{"Little Red", "2222", "1"});
(4) Check
String str = "SELECT * from student";
cursor cursor = mdb.rawquery (str,null);
while (Cursor.movetonext ()) {
Get the index of each field value
int idindex = Cursor.getcolumnindex ("id");
int nameindex = Cursor.getcolumnindex ("name");
int phoneindex = Cursor.getcolumnindex ("Phone");
Extracting data based on index
String id = cursor.getstring (idindex);
String name = cursor.getstring (Nameindex);
String phone = cursor.getstring (Phoneindex);
}
ORM Object Relational Mapping
(1) Increase
Contentvalues values = new Contentsvalues ();
Values.put ("name", "small white");
Values.put ("Phone", "12356");
Mdb.insert ("Student",//table name
null;//Avoiding Insert Errors
values);//The value inserted
(2) by deleting
Mdb.delete ("Student",//table name
"Name=?", conditional statement
New string[]{"Small white"}); placeholder padding
(3) Change
Contentvalues values = new Contentvalues ();
Values.put ("Phone", "45621");
Mdb.update ("Student",//table name
values,//the value that will be modified
"Name=?", conditional statement
New string[]{"Small white"});//Placeholder padding
(4) Check
cursor cursor = mdb.query ("Student",//table name
Fields for null,//queries
null,//Query criteria
null,//the fill of a conditional statement placeholder
null,//grouping statements
null,//padding for grouping statement placeholders
NULL);//Sort statement
while (Cursor.movetonext ()) {
Get field Index
int nameindex = Cursor.getcolumnindex ("name");
Extracting data based on index
String name = cursor.getstring (Nameindex);
}
CursorAdapter Leng Adapter
Note: Must contain _id
Converting a layout XML file into a layout object
@Override
Public View Newview (context context, cursor cursor, viewgroup parent) {
View inflate = Getlayoutinflater (). Inflate (R.layout.list_item, NULL);
return inflate;
}
Set up data display
@Override
public void BindView (view view, context context, cursor cursor) {
TextView textView1 = (TextView) View.findviewbyid (R.ID.TEXTVIEW1);
TextView textView2 = (TextView) View.findviewbyid (R.ID.TEXTVIEW2);
Get Data
String name = cursor.getstring (Cursor.getcolumnindex ("name"));
String phone = cursor.getstring (Cursor.getcolumnindex ("Phone"));
Setting property values
Textview1.settext (name);
Textview2.settext (phone);
}
Update data
Re-query to get a new cursor object
cursor cursor = mdb.query ("student", NULL, NULL, NULL, NULL, NULL, NULL);
To set the latest cursor object to the adapter
Madapter.changecursor (cursor);
Refresh Display
Madapter.notifydatasetchanged ();