android app development 05 : saving data

43
Saving Data Anuchit Chalothorn [email protected] 5 Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Upload: anuchit-chalothorn

Post on 06-May-2015

1.769 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Android App Development 05 : Saving Data

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

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

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
Page 5: Android App Development 05 : Saving Data

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

Workshop: Setting Activity

Create Setting Activity to store username and password in shared preference data

Page 8: Android App Development 05 : Saving Data

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

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

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

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

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 14: Android App Development 05 : Saving Data

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 16: Android App Development 05 : Saving Data

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

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

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 20: Android App Development 05 : Saving Data

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 22: Android App Development 05 : Saving Data

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 24: Android App Development 05 : Saving Data

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 26: Android App Development 05 : Saving Data

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

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

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

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

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

Workshop: Select

Use db.query to query your data and return into Array.

Page 33: Android App Development 05 : Saving Data

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 35: Android App Development 05 : Saving Data

Workshop: Update

Use a db.update() method to update data in specific record and criteria.

Page 37: Android App Development 05 : Saving Data

Workshop: Delete

Use a db.delete() method to delete data in specific record and criteria.

Page 39: Android App Development 05 : Saving Data

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 41: Android App Development 05 : Saving Data

Push data to a ListView

Query data and return to an ArrayList then push ArrayList to ListView.

Page 43: Android App Development 05 : Saving Data

End