You are on page 1of 14

ANDROID APPLICATION DEVELOPMENT

Working with DATA

hoccungdoanhnghiep.com

Ni dung
Data Storage in Android

Shared Preferences
SQLite database

hoccungdoanhnghiep.com

Data Storage in Android


Android cung cp nhiu c ch lu d liu,

v ty vo nhu cu m LTV la chn phng n hp l Cc c ch lu d liu:


Shared Preferences: Dng lu cc d liu

nguyn thy dng cp: key/value Internal Storage: Lu d liu trn b nh trong ca thit b External Storage: Lu tr d liu b nh ngoi (th nh, external mounted partition) SQLite database: lu d liu dng c cu trc trong mt c s d liu hoccungdoanhnghiep.com Network connection: kt ni n server mng v

Shared Preferences
Class SharedPreferences cung cp sn b

framework save & get ra cp key-value lu trc Cc dng d liu c bn c dng vi sharepref: int, string, float, long, boolean

hoccungdoanhnghiep.com

SharedPreferences -> lu/edit data


Khai bo string refPath o truy xut data: String PREF_MY_INFO_PATH = "vn.com.smartservice.myinfo Khai bo cc key string lu values tng ng String PREF_NAME = "PREF_NAME String PREF_AGE = "PREF_AGE String PREF_SEX = "PREF_SEX activity L6DataStorageActivity, cn save data, thc hin cc vic sau: Khi to object ca class SharePreference: SharedPreferences myinfosets = getSharedPreferences(PREF_MY_INFO_PATH , 0); Khi to object editor c kh nng save data: SharedPreferences.Editor editor = myinfosets.edit(); save data bng cch gi lnh put[TypeOfData](key, value) editor.putBoolean(PREF_SEX, true/false); editor.commit(); // save data

hoccungdoanhnghiep.com

SharedPreferences -> get saved data


activity ViewMyInfo, ta ly ra data lu sn t trc. Thc hin

cc vic sau:
Khi to object ca class SharePreference:

SharedPreferences myinfosets = getSharedPreferences(PREF_MY_INFO_PATH , 0);


Dng obj get ra data tng ng c lu:

boolean sex = myinfosets.getBoolean(PREF_SEX, false); //

trong trng hp tm khng thy d liu c lu, false s c t ng tr v


Tng t cho cc kiu data khc

hoccungdoanhnghiep.com

S dng SQLite database


Android h tr hon ton SQLite databases: create db, create

table, insert, delete, update, select. SQLite db, tham kho: http://www.sqlite.org/ Cc datatypes cung cp bi sqlite: http://www.sqlite.org/datatypes.html Cch tt nht l tha k t lp SQLiteOpenHelper thao tc vi db:

thi im onCreate, db ch c to mi khi cha

tn ti db no a vo khi to cc bng onCreate Db khng c load ln cho n khi c mt yu cu truy xut vo db


read/write vi db, gi vo cc hm getWritableDatabase() v

getReadableDatabase() hoccungdoanhnghiep.com

To lp x l db tha k SQLiteOpenHelper
File > New > Class
Name: DatabaseHelper Package: vn.com.smartservice.lib SupperClass: SQLiteOpenHelper M file DatabaseHelper.java va to, Override

cc hm onCreate, onUpgrade s dng


File > New > Class > MyNoteAdapter.java
Class MyNoteAdapter.java s ng vai tr layer

access db Vit cc hm truy xut, insert, d liu cn thit


hoccungdoanhnghiep.com

To mi SQLite database
M file DatabaseHelper.java:
Override hm onCreate Chy query khi to db: "CREATE TABLE

mynotes (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, content TEXT);"

hoccungdoanhnghiep.com

Write d liu vo db
write d liu, phi c vo db dng

writetable thng qua vic gi hm: getWritableDatabase():


M file MyNoteAdapter.java

Create method: createNote(String title, String content) lm

cc vic sau: kt ni n db dng write: DatabaseHelper. getWritableDatabase() To object ca i tng ContentValues put vo set cc data: ContentValues cv = new ContentValues(); put data vo: o cv.put("title", title); o cv.put("content", content); gi lnh insert d liu: db.insert(tn-bng,null,cv) hoccungdoanhnghiep.com ng kt ni bng cch gi vo db.close()

Ly d liu t db ra
Android s dng lp Cursor lu d liu ly

c t db ra. Cursor ng vai tr l 1 result set M file MyNoteAdapter.java Create method: getNote()
Query db: database.rawQuery("select * from mynotes

", null);

T kt qu tr v, ly ra d liu dng u tin v

hin th: Toast.makeText(getApplicationContext(), c.getString(1) + "," + c.getString(2), 5)

Cn ch , trong qu trnh read data, cn phi gi


hoccungdoanhnghiep.com

connection lun m. Do , ch c d liu, ta nn gi vo hm: getReadableDatabase()

S dng db to sn
Copy file db to vo th mc /assets/ trong

project Khi thc hin truy cp db, ta phi copy db ny vo th mc: /data/data/package-name/databases/ Sau khi copy thnh cng, vic truy cp c th c thc hin

hoccungdoanhnghiep.com

Bi tp v nh
Pht trin tip phn MyInfo:
Thay vic g age = thanh seekbar i mn hnh hin th My Info ln trc, thng bo

ngi dng cha a info vo Thm Toast thng bo kt qu


Pht trin tip phn My Note
Vit hm ly ra MyNote c id bt k save
Hin thi ln mn hnh Edit note Save Thm Toast thng bo vic save hon tt/c li
hoccungdoanhnghiep.com

Tham kho
http://developer.android.com/guide/topics/data/dat

a-storage.html

hoccungdoanhnghiep.com

You might also like