android app development 05 : saving data

Post on 06-May-2015

1.769 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Saving DataAnuchit Chalothornanoochit@gmail.com

5

Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Data Storage

Android provides several options for you to save persistent application data. ● Share Preference● Internal storage● External Storage● SQLite Database● Network Connection

Shared Preference

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types.

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

Workshop: Setting Activity

Create Setting Activity to store username and password in shared preference 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.

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.

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

Environment Variable

● DIRECTORY_ALARMS● DIRECTORY_DCIM● DIRECTORY_DOWNLOADS● DIRECTORY_MOVIES● DIRECTORY_MUSIC● DIRECTORY_NOTIFICATIONS● DIRECTORY_PICTURES● DIRECTORY_PODCASTS● DIRECTORY_RINGTONES

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);

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.

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.

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.

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

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

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

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);

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.

Query Example

Method rawQuery() Example

Cursor cursor = getReadableDatabase(). rawQuery("select * from todo where _id = ?", new String[] {id});

Query Example

Method query() Example

database.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_CATEGORY, KEY_SUMMARY, KEY_DESCRIPTION }, null, null, null, null, null);

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.

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.

Workshop: Select

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

Workshop: Select All

Two ways to return select all records depend on return data; Array and ArrayList. Use db.rawQuery to query your data.

Workshop: Update

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

Workshop: Delete

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

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.

Push data to a ListView

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

End

top related