Download - Android App Development 05 : Saving Data
![Page 1: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/1.jpg)
Saving DataAnuchit [email protected]
5
Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
![Page 2: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/2.jpg)
Data Storage
Android provides several options for you to save persistent application data. ● Share Preference● Internal storage● External Storage● SQLite Database● Network Connection
![Page 3: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/3.jpg)
Shared Preference
The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types.
![Page 4: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/4.jpg)
![Page 5: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/5.jpg)
Setting Activity
Applications often include settings that allow users to modify app features and behaviors. You should use Android's Preference APIs to build an interface that's consistent with the user experience in other Android apps (including the system settings).
Ref: http://developer.android.com/guide/topics/ui/settings.html
![Page 6: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/6.jpg)
Workshop: Setting Activity
Create Setting Activity to store username and password in shared preference data
![Page 7: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/7.jpg)
![Page 8: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/8.jpg)
Internal Storage
Access to the file system is performed via the standard java.io classes. Android provides also helper classes for creating and accessing new files and directories.
![Page 9: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/9.jpg)
Internal Storage
For example the getDir(String, int) method would create or access a directory. The openFileInput(String s) method would open a file for input and openFileOutput(String s, int) would create a file.
![Page 10: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/10.jpg)
Permission Mode
int specifies the permissions which are:● MODE_PRIVATE
○ No access for other applications● MODE_WORLD_READABLE
○ Read access for other applications● MODE_WORLD_WRITABLE
○ Write access for other applications● MODE_WORLD_READABLE | MODE_WORLD_WRITABLE
○ Read / Write access
![Page 11: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/11.jpg)
Environment Variable
● DIRECTORY_ALARMS● DIRECTORY_DCIM● DIRECTORY_DOWNLOADS● DIRECTORY_MOVIES● DIRECTORY_MUSIC● DIRECTORY_NOTIFICATIONS● DIRECTORY_PICTURES● DIRECTORY_PODCASTS● DIRECTORY_RINGTONES
![Page 12: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/12.jpg)
Workshop: Get your path
Android provide methods to get path from external storage and internal storage
File internal_path,external_path;internal_path=getDir(Environment.DIRECTORY_DOWNLOADS, 0);external_path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
![Page 13: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/13.jpg)
![Page 14: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/14.jpg)
SQLite
SQLite is an Open Source Database which is embedded into Android. SQLite supports standard relational database features like SQL syntax, transactions and prepared statements.
![Page 15: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/15.jpg)
Useful Tools
● SQLite database browser● SQLite Manager
![Page 16: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/16.jpg)
SQLite in Android
SQLite is available on every Android device,does not require any database setup or administration. You only have to define the SQL statements for creating and updating the database.
![Page 17: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/17.jpg)
SQLite Open Helper
To create and upgrade a database in your Android application you usually subclass SQLiteOpenHelper. In the constructor of your subclass you call the super() method of SQLiteOpenHelper, specifying the database name and the current database version.
![Page 18: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/18.jpg)
Workshop: SQLite Open Helper
Create blank app with database helper class extend from SQLiteOpenHelper class, identify database name, version number and create table at onCreate method. Call database helper from main activity. Your database will create at /data/data/packagename/files/databasename
![Page 19: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/19.jpg)
![Page 20: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/20.jpg)
Workshop: SQLite External Storage
Create blank app with database helper class extend from SQLiteOpenHelper class, identify database name which store in external storage files directory, version number and create table at onCreate method. Call database helper from main activity. Your database will create at /sdcard/Android/data/packagename/files/databasename.db
![Page 21: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/21.jpg)
![Page 22: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/22.jpg)
Workshop: CRUD
Create an app to store, select, update and delete. Using following scenarios:● Insert 2 rows
○ 1, Jim, 023645456○ 2, John, 023645876
● Select John● Select all records● Update Jim's number to 024953658● Delete John
![Page 23: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/23.jpg)
![Page 24: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/24.jpg)
Workshop: Insert Data
Use insert() method to insert data in SQLite table.
ContentValues val = new ContentValues();val.put("name", name);val.put("tel", tel);long row = db.insert("member", null, val);
![Page 25: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/25.jpg)
![Page 26: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/26.jpg)
Query
Queries can be created via the rawQuery() and query() methods or via the SQLiteQueryBuilder class .● rawQuery() directly accepts an SQL select
statement as input.● query() provides a structured interface for
specifying the SQL query.
![Page 27: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/27.jpg)
Query Example
Method rawQuery() Example
Cursor cursor = getReadableDatabase(). rawQuery("select * from todo where _id = ?", new String[] {id});
![Page 28: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/28.jpg)
Query Example
Method query() Example
database.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION }, null, null, null, null, null);
![Page 29: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/29.jpg)
Cursor
A query returns a Cursor object. A Cursor represents the result of a query and basically points to one row of the query result. This way Android can buffer the query results efficiently; as it does not have to load all data into memory.
![Page 30: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/30.jpg)
Cursor
To move between individual data rows, you can use the moveToFirst() and moveToNext() methods. The isAfterLast() method allows to check if the end of the query result has been reached.
![Page 31: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/31.jpg)
Workshop: Select
Use db.query to query your data and return into Array.
![Page 32: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/32.jpg)
![Page 33: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/33.jpg)
Workshop: Select All
Two ways to return select all records depend on return data; Array and ArrayList. Use db.rawQuery to query your data.
![Page 34: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/34.jpg)
![Page 35: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/35.jpg)
Workshop: Update
Use a db.update() method to update data in specific record and criteria.
![Page 36: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/36.jpg)
![Page 37: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/37.jpg)
Workshop: Delete
Use a db.delete() method to delete data in specific record and criteria.
![Page 38: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/38.jpg)
![Page 39: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/39.jpg)
Put it together in real life!
You may pre load data in the app or try to update data from internet, so the easy way put it in asset and copy to another storage.
![Page 40: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/40.jpg)
![Page 41: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/41.jpg)
Push data to a ListView
Query data and return to an ArrayList then push ArrayList to ListView.
![Page 42: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/42.jpg)
![Page 43: Android App Development 05 : Saving Data](https://reader033.vdocuments.net/reader033/viewer/2022052504/554a1f6eb4c9055c598b5836/html5/thumbnails/43.jpg)
End