Android Learning notes-eighth data storage

Source: Internet
Author: User

Chapter Eighth data storage

  1. How data is stored

    Internal Storage Internal Storage

    External Storage External Storage

    SQLite Database Storage

    Http Network Storage

  2. 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);

  3. Internal Storage Internal Storage

    Storage location: data/data/Package Name/files/xxx.xxx

    Format: Custom

    Save data:

    FileOutputStream fos = openfileoutput ("AAA", 0);

    String str = "data store";

    Fos.write (Str.getbytes ());

    Reading data

    FileInputStream FIS = openfileinput ("AAA");

    Bytearraybuffer ArrayBuffer = new Bytearraybuffer (100);

    Byte[] B = new byte[1024];

    int len = 0;

    while ( -1!= (Len=fis.read (b))) {

    Arraybuffer.append (B,0,len);

    }

    String str = new String (Arraybuffer.tobytearray ());

  4. External Storage External Storage

    Storage location: Mnt/sdcard

    Read permissions:

    (1) SDcard path:

    Environment.getexternalstoragedirectory (). GetAbsolutePath ();

    (2) Determine if SDcard is available:

    Environment.MEDIA_MOUNTED.equals (Environment.getexternalstoragestate ());

  5. 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);

    }

  6. 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 ();

Android Learning note-eighth data storage

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.