"Android Official document Reading notes" save data

Source: Internet
Author: User

Android offers 3 ways to save data:sharepreference, files, and databases.

1,sharepreference

If you want to save a relatively small collection of key-values, you can use the Sharedpreferences API. The Sharedpreferences object points to a file that contains key-value pairs and provides a simple way to read and write. Each sharedpreferences file is managed by the framework and is available for private or shared use. Its essence is an XML file.

Data read-in:

Sharedpreferences sharedpref = getactivity (). Getpreferences (Context.mode_private);

Long highscore = Sharedpref.getint (key, defaultvalue);

Data write:

Sharedpreferences sharedpref = getactivity (). Getpreferences (Context.mode_private);
Sharedpreferences.editor Editor = Sharedpref.edit ();
Editor.putint (GetString (R.string.saved_high_score), newhighscore);
Editor.commit ();


2, File

The file object is suitable for reading or writing large amounts of data in Start-to-finish, the file object is suitable for reading or writing large amounts of data, but the disadvantage is that it is difficult to update the data. Android file storage is divided into internal storage and external storage. Internal storage features: always available; the saved file can only be used for the default application, and when the user uninstalls the application, all files from the application are deleted from the internal storage. Therefore, internal storage is undoubtedly the best choice when you want to ensure that files are accessible to users and other applications. External storage Features: not always available, with global readability, so the saved file may be read by someone outside the control range; The user wants to uninstall the application, and the application files are deleted only when the application files are saved in the Getexternalfilesdir () directory. Therefore, external storage is undoubtedly the best approach for files that do not require access restrictions, and files that you want to share with other applications or allow users to access using a computer.

When storing files in the internal store, you can call one of two methods to get the appropriate catalog files:
Getfilesdir () returns the file that represents the internal directory of the application
Getcachedir () returns a file representing the internal directory of the application's temporary cache file.


Permissions for external storage:

<manifest ...>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
...
</manifest>


3, Database

< Span style= "font-family: ' Microsoft Yahei '; font-size:13px; line-height:19px; Background-color:rgb (254,254,242) "> Android uses SQLite to support database operations, SQLite is a lightweight database that supports basic SQL syntax and is often used as a way of storing data. Android provides a class named Sqlitedatabase for this database, encapsulating some of the APIs that manipulate the database. General Duplicate or structured data is saved using a database.

Data Read Example:

Cursor C = Db.query (
Feedentry.table_name,//the TABLE to query
Projection,//the columns to return
Selection,//The columns for the WHERE clause
Selectionargs,//The values for the WHERE clause
NULL,//don ' t group the rows
NULL,//don ' t filter by row groups
SortOrder//the sort order
);


Update Data Write Library example:

Sqlitedatabase db = Mdbhelper.getreadabledatabase ();

Contentvalues values = new Contentvalues ();//New value for one column
Values.put (Feedentry.column_name_title, TITLE);//which row to update, based on the ID

String selection = feedentry.column_name_entry_id + "like?";
String[] Selectionargs = {string.valueof (rowId)};

int count = Db.update (FeedReaderDbHelper.FeedEntry.TABLE_NAME, values, selection, Selectionargs);



"Android Official document Reading notes" save data

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.