Professional Documents
Culture Documents
Androidapplicationdevelopment6 120506104223 Phpapp01
Androidapplicationdevelopment6 120506104223 Phpapp01
hoccungdoanhnghiep.com
Ni dung
Data Storage in Android
Shared Preferences
SQLite database
hoccungdoanhnghiep.com
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
hoccungdoanhnghiep.com
cc vic sau:
Khi to object ca class SharePreference:
hoccungdoanhnghiep.com
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:
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
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
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);
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
Tham kho
http://developer.android.com/guide/topics/data/dat
a-storage.html
hoccungdoanhnghiep.com