Chuanzhi podcast-pull parses XML files, sharedpreferences, SQLite Database

Source: Internet
Author: User

As we learn more deeply, the content of these days has become more and more exciting. as an application developer, We have to operate data. android provides us with many types of data storage and reading.
This method reduces the development cycle and improves the system's robustness during development.
Today, we will learn how to use the XML parsing technology pull in the Android system.
Sharedpreferences, a class for storing and reading software parameters, and the use and related operations of embedded relational database SQLite provided in the system.

1. Use pull to parse XML files

This
This is the method used in the Android system. We recommend that you use it.
1) read the used
Main Methods:

Get a parser: xmlpullparser parser = xml. newpullparser ();
Upload
Enter an input stream: parser. setinput (instream, "UTF-8 ");
Get event class
Type: parser. geteventtype ();
Determine if it is the end of the file: xmlpullparser. end_document
Judge whether
Yesfile start: xmlpullparser. start_document:
Determine whether the tag is started: xmlpullparser. start_tag
Judgement
Whether the disconnection ends with a tag: xmlpullparser. end_tag
Push the pointer down: parser. Next ();
2) The main method used for writing:

Initialization Tool
Class: xmlserializer serializer = xml. newserializer ();
Input
Stream: serializer. setoutput (writer );
Configure the document
Set: serializer. startdocument ("UTF-8", true );
Documentation
Start: serializer. starttag ("", "persons ");
Set tags
Sex: serializer. Attribute ("", "ID", String. valueof (person. GETID ()));
Settings
This information: serializer. Text (string. valueof (person. getage ()));
Set the tag
Bundle: serializer. endtag ("", "Age ");
Set document end: serializer. enddocument ();

2. Use sharedpreferences
Store Data

1) Write parameters:
Initialize the tool class: sharedpreferences =
Getsharedpreferences ("itcast", context. mode_private );
Get Editor: Editor
Editor = sharedpreferences. Edit ()
Set related parameters: Editor. putstring ("name ","
Chuanzhi podcast ");
Submit changes: Editor. Commit ();
2) read parameters:
String name =
Sharedpreferences. getstring ("name ","");
Summary:

-- Especially suitable for saving software configuration parameters
-- Actually, data is stored in XML files.
-- Files are stored in
/Data/<package name>/shared_prefs directory
-- Do not forget to submit, no
It does not take effect.
-- Whether or not the read permission is granted during the creation of the read operation.
-- If this key does not exist in preference
Will return the default value

3. SQLite
Database storage data

1) sqliteopenhelper
By inheriting this class, rewrite the following two methods to control the database version.

If the program is executed for the first time and no database exists, run this method to create the database: oncreate ()
For example
If the database version already exists or is earlier than the created version, run this method to update the database: onupgrade ()
Open the database in writing mode. If it is full, report
Error: getwritabledatabase ()
Open the database in read/write mode. If it is full, read-only: getreadabledatabase ()
2) operate databases

For example:
Sqlitedatabase
DB = ....;
Db.exe csql ("insert into person (name, age) values ('chuanzhi pods ',
4 )");
DB. Close ();
-Execute an SQL statement and add, delete, and modify it: db.exe csql ("insert
Person (name, age) values (?,?) ", New object [] {" Chuanzhi podcast ", 4 });
-Execute SQL statements,
Query: DB. rawquery ("select * From person where name like? And age =? ",
New String [] {"% Chuanzhi %", "4 "});
-Insert: DB. insert ("person", null, values );
-
Delete: DB. Delete ("person", "personid <? ", New string [] {" 2 "});
-More
New: DB. Update ("person", values, "personid =? ", New string [] {" 1 "});
-Query
Query: DB. Query ("person", new string [] {"personid, name, age"}, "name like? ",
New String [] {"% Chuanzhi %"}, null, null,
"Personid DESC", "1, 2 ");
3) transaction operations

-Enable event
Thing: DB. begintransaction ();
-When this method is called, The endtransaction () is executed ()
The current transaction is committed. If this method is not called, the transaction will be rolled back.
-The transaction identifier determines whether to commit the transaction or roll back the transaction: DB. endtransaction ()
Summary:

-- This is an embedded relational database.
-- What type of storage
Any data can be used in databases with weak data types.
-- The field defined as integer primary key can only store 64-bit Integers
-- Execute onupgrade ()
Method when updating the database, pay attention to the backup of the original data
-- Database operations are performed by a single user, so we recommend that you do not close the database to improve efficiency.

Related Article

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.