1. 5
Saving Data
Anuchit Chalothorn
anoochit@gmail.com
Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
2. Data Storage
Android provides several options for you to
save persistent application data.
● Share Preference
● Internal storage
● External Storage
● SQLite Database
● Network Connection
3. Shared Preference
The SharedPreferences class provides a
general framework that allows you to save and
retrieve persistent key-value pairs of primitive
data types.
5. 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
8. 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.
9. 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.
10. 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
12. 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);
14. 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.
16. 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.
17. 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.
18. 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
20. 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
22. 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
24. 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);
26. 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.
27. Query Example
Method rawQuery() Example
Cursor cursor = getReadableDatabase().
rawQuery("select * from todo where _id =
?", new String[] {id});
28. Query Example
Method query() Example
database.query(DATABASE_TABLE,
new String[] { KEY_ROWID, KEY_CATEGORY,
KEY_SUMMARY, KEY_DESCRIPTION },
null, null, null, null, null);
29. 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.
30. 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.
33. Workshop: Select All
Two ways to return select all records depend
on return data; Array and ArrayList. Use db.
rawQuery to query your data.
35. Workshop: Update
Use a db.update() method to update data in
specific record and criteria.
37. Workshop: Delete
Use a db.delete() method to delete data in
specific record and criteria.
39. 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.
41. Push data to a ListView
Query data and return to an ArrayList then
push ArrayList to ListView.