Andrews notes: Data storage and IO

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_20539533/article/details/102759857

Android built-in SQLite database.

SQLite database: a lightweight database, no background process, the entire database will correspond to a file, to facilitate transplantation.

 

1.SharedPreferences

SharedPreferences data stored in the main configuration is similar to the data format. The saved data is mainly simple type of key-value pairs.

SharedPreferences interface is mainly responsible for reading the application's Preferences data, provided as follows.

  • boolean contains (String key): SharedPreferences determines whether the key contains data specific
  • Map <String,?> GetAll (): Gets SharedPreferences data in all of the key-value pairs
  • getXxx (String key, xxx defValue): SharedPreferences acquiring data corresponding to the specified key in the value. If the key does not exist, return the default value defValue. xxx can be boolean \ float \ int \ long \ String, etc.

SharedPreferences interface does not provide the ability to write data, by writing SharedPreferences.Editor allowed, Editor SharedPreferences target acquisition article with its corresponding edit () method.

  • SharedPreferences.Editor.clean (): Clear all data in SharedPreferences
  • SharedPreferences.Editor.putXxx (String key, xxx value): stored data corresponding to the specified key SharedPreferences
  • SharedPreferences.Editor.remove (String key): delete the specified key SharedPreferences corresponding data item
  • boolean apply (): When the Editor editing is complete, the method is called to submit changes. commit is to commit changes (submitted immediately). apply () is submitted to modify the background, it does not block the foreground thread, recommended apply () method.

Creating instance SharedPreferences

Context provided by getSharedPreferences (String name, int mode) Gets SharedPreferences instance method (SharedPreferences instance can not be created directly) the second parameter set Context.MODE_PRIVATE. Data can only be read specified SharedPreferences present application. The other two do not recommend, do not write.

2.File storage

Java provides a complete set of IO stream system, a FileInputStream \ FileOutputStream and so on, you can easily access the contents of the file on disk IO by these six or Android to support these.

Context provides two ways to open the data file for the application folder in the file IO streams

  • FileInputStream openFileInput (String name): Open the application data folder name corresponding to the file input stream.
  • FileOutputStream openFileOutput (String name, int mode): application to open the output data stream folder corresponding to the file name.

There mode

  • MODE_PRIVATE: This file can only be read and the current program.
  • MODE_APPEND: open the file in append mode, the application can append to the file contents.
  • MODE_WORLD_READABLE: the contents of the file can be read by other programs.
  • MODE_WORLD_WRITEABLE: the contents of this file by other programs to read and write.

Other methods

  • getDir (String name, int mode): Obtain or create a subdirectory under the name corresponding to the application data folder
  • File getFilesDir (): Gets the absolute path of the application data folder
  • String [] fileList (): all files returned to the application data folder
  • deleteFile (String): data file deletion application's folder under the specified file

Read files on the SD card (SD card 1. 2. Add the permissions to read and write the SD card in the manifest file of the application (AndroidManifest.xml) in)

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

1. Request dynamic access permissions to read and write SD card

Example:

requestPermissions(new int[] {Manifest.permission.WRITE_EXTERNAL_STORAGE},0x123)

2. Call the Environment of getExternalStorageDirectory () method to get an external memory (SD card acquiring directory)

3. Use FileInputStream FileOutputStream \ files \ FileReader FileWriter or SD card reader

3.SQLite database

SQLiteDatabase

  • static SQLiteDatabase openDatabase (String path, SQLiteDatabase.CursorFactor factory, int flages): Open the SQLite database file path represents.
  • static SQLiteDatabase openOrCreateDatabase (File file, SQliteDatabase.Cursor Factory factory): Open or create (if not present) SQLite database file file represents.
  • static SQLiteDatabase openOrCreateDatabase (String path, SQLiteDatabase.Cursor Factory factory): Open or create a SQLite database (if not present) path file represents.

After obtaining the SQLiteDatabase object program, a database may be operated by the following method

  • execSQL (String sql, Object [] bindArgs): execute SQL statements with placeholders
  • execSQL (String sql): execute SQL statements
  • insert (String table, String nullColumnHack, ContentValues ​​values): to insert data into the specified table
  • update (String table, ContentValues ​​values, String whereClause, String [] whereArgs): update a specific data specified in the table
  • delete (String table, String whereClause, String [] whereAtgs): delete specific data specified in the table
  • Cursor query (String sql, String [] columns, String whereClause, String [] whereArgs, String groupBy, String having, String orderBy): execute the query to specify the data table
  • Cursor query (String sql, String [] columns, String whereClause, String [] whereArgs, String groupBy, String having, String orderBy, String limit):. Query the specified data tables wherein performing the first parameter value to control whether deduplication
  • rawQuery (String sql, String [] selectionArgs): SQL query execution with placeholder.
  • beginTransaction (): Begin a transaction

Cursor provides a method to move the record pointer query results

  • move (int offset): the record pointer upward or downward movement of the specified number of lines (down positive, negative direction)
  • boolean moveToFirst (): the record pointer to the first line, if the mobile Returns true if successful
  • boolean moveToLast (): the record pointer to the last line, if the mobile Returns true if successful
  • boolean moveToNext (): the record pointer to the next line, if the mobile Returns true if successful
  • boolean moveToPosition (int position): the record pointer to a specific line, if the mobile Returns true if successful
  • boolean moveToPrevious (): the record pointer to the line, if the mobile Returns true if successful

SQLiteOpenHelper类

Usually inherit SQLiteOpenHelper development subclass, and () \ getWritableDatabase () method of the subclass by getReadableDatabase broke database

4. gesture (Gesture)

Andrews need to pass the time as a gesture detection provides a GestureDetector class, GestureDetector instance represents a gesture detection, create a GestureDetectorListener GestureDetector example, GestureDetector.OnGestureListener is a listener, is responsible for providing a response to the behavior of the user gesture.

use

1. Create a GestureDetector objects (GestureDetector.OnGestureListener must implement listener interfaces)

2. Application of Activity (a specific component) of TouchEvent event binding listener, specify the TouchEvent events on (specific components) Activity GestureDetector to deal with an event handler.

Guess you like

Origin blog.csdn.net/qq_20539533/article/details/102759857