String stringPreference=sharedPre.getString “textEntryValue”, “”;... Insert:Ví d : //Create a new row of values to insert ContentValues newRow = new ContentValues; //Asign values for eac
Trang 1L U TR VÀ TRUY XU T D LI U
TRONG NG D NG
Ch ng 10
Lê V n H nh
levanhanhvn@gmail.com
Trang 31 SharedPreferences
• L u tr d li u d ng c b n
• Ch h tr m t s ki u d li u c b n: booleans, float,
int, long, string
• Ho t đ ng theo ki u l u tr c ng nh truy xu t thông
qua c p giá tr key/value
• Các d li u đ c l u tr và truy xu t qua t ng phiên
(session) t ng tác c a ng d ng v i ng i dùng
• S d ng đ i t ng l p SharedPreference thông qua
ph ng th c:
GetsharedPreferences(string, int)
Trang 4 MODE_PRIVATE: mode ghi m c đ nh, ch có duy nh t
ng d ng t o ra t p tin đ c phép truy c p vào.
MODE_WORLD_READABLE: cho phép các ng d ng
khác truy c p vào.
khác truy c p và s a đ i.
MODE_MULTI_PROCESS: cho phép nhi u ti n trình
trên ng d ng cùng truy xu t vào cùng m t th i đi m.
Trang 51 SharedPreferences
• B sung thông tin cho đ i t ng SharedPreferences():
thông qua đ i t ng Editor
Ví d :
SharedPreferences.Editor editor = sharedPre edit();
editor.putBoolean (“isTrue”, true);
Trang 6String stringPreference=
sharedPre.getString (“textEntryValue”, “”);
Trang 72 B L U TR TRONG
• L u d li u riêng trên b nh thi t b
• Khi ng d ng đ c cài đ t, h th ng cung c p 1 vùng
b nh trong dành cho vi c l u tr d li u cho m i
ng d ng riêng
• Khi ng i dùng g b ng d ng, vùng b nh này s
đ c xóa
• T o và l u tr file vào b l u tr trong
o G i openFileOutput() v i tên file và ch đ truy c p file
o Ghi lên file s d ng ph ng th c write()
Trang 82 B L U TR TRONG
• Ví d 1:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME,
Context.MODE_PRIVATE); fos.write(string.getBytes());
fos.close();
Trang 93 B L U TR NGOÀI
• L u tr d li u public trên b nh ngoài
• M i thi t b Android có th h tr b l u tr ngoài s
Trang 103 B L U TR NGOÀI
• Ví d :
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
Trang 114 SQLite Database
4.1 Gi i thi u
• SQLite là c s d li u m đ c nhúng vào Android,
h tr các đ c đi m v quan h chu n c a c s d
li u nh cú pháp, transaction, các câu l nh
• S d ng SQLite không yêu c u v thi t l p b t c c
s d li u ho c đòi h i quy n admin
• H tr các ki u d li u: TEXT, INTEGER, REAL
Trang 12• SQLiteDatabase cung c p ph ng th c insert(),
update(), delete(), ho c execSQL() cho phép th c hi n
truy xu t d li u
4 SQLite Database
Trang 134.2 T oDatabase:
• Ví d :
private static final String DatabaseName = “myDatabase.db”; private static final String DatabaseTable = “mainTable”;
private static final int DatabaseVersion = 1;
//The name and column index of each column in your database public static final String KeyName = “name”;
public static final int NameColumn = 1;
//SQL Statement to create a new database private static final String DatabaseCreate = “create table”
+ DatabaseTable + “ (” + KEY_ID + “integer primary key
autoincrement , ” + KeyName + “text not null);”;
@Override public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DatabaseCreate);
}
4 SQLite Database
Trang 144.3 M Database:
SQLitDatabase db;
try {
db=dbHelper.getWritableDatabase(); }
catch (SQLiteException ex) {
db = dbHelper.getReadableDatabase();
}
4 SQLite Database
Trang 154.4 M t s ph ng th c đ đi u khi n Cursor thao tác
Trang 16return database.query(String table,
String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy);
• Trong đó:
String TABLE: tên c a b ng c n truy v n
String[] columns: danh sách các c t s tr v d li u
String selection: ch a các đi u ki n truy v n
String[] selectionArgs: danh sách các tham s ph cho câu đi u ki n
String[] groupBy: gom nhóm các c t k t qu
String[] having : b l c theo đi u ki n
String[] orderBy : s p x p theo m ng c t đ c ch đ nh
4 SQLite Database
Trang 174.5 Truy v n Database:
Ví d :
//Return all rows for columns one and three, no duplicates
String[] ResultColumns = new String[] {KeyID,
KeyCol1, KeyCol3};
Cursor allRows = myDatabase.query(true, DatabaseTable,
ResultColumns, null , null , null , null , null , null);
/*Return all columns for rows where column 3 equal a set value and rows are ordered by column 5 */
String where = KeyCol3 + “=” + requiredValue;
String order = KeyCol5;
Cursor myResult = myDatabase.query(DatabaseTable, null,
where, null,null, null, order)
4 SQLite Database
Trang 184.6 Insert:
Ví d :
//Create a new row of values to insert
ContentValues newRow = new ContentValues();
//Asign values for each row
newRow.put(ColumnName1, newValue1);
newRow.put(ColumnName2, newValue2);
//Repeat for each column
//Insert the row into your table
myDatabase.insert (DatabaseTable, null, newRow);
4 SQLite Database
Trang 194.7 Update:
Ví d :
//Create the updated row content
ContentValues UpdateRow = new ContentValues();
//Asign values for each row
UpdateRow.put(ColumnName1, newValue1);
UpdateRow.put(ColumnName2, newValue2);
//Repeat for each column
//define conditional
String where = KeyID + ”=” + rowID;
//Insert the row into your table
myDatabase.update(DatabaseTable, newRow, where, null);
4 SQLite Database
Trang 204.7 Delete:
• S d ng câu truy v n delete truy n vào tên b ng và ch
s c a dòng c n xóa trong m nh đ where
• N u m nh đ where không có s th c hi n xóa t t c các
Trang 21• Contact (people, phones, photos, group)
• MediaStore (music, video, image)
• Setting
Trang 22• C n khai báo trong file AndroidManifest.xml cho bi t
ng d ng s d ng nh ng content provider nào
<provider android:name =“SomeProvider
android:authorities =“com.your_company.SomeProvider”/>
<provider android:name =“NotePadProvider
android:authorities =“com.google.provider.NotePad”/>
Trang 23Cursor
Trang 24//The cursor is already pointing to the first row
//lets access a few columns
int nameColumnIndex = cur.getColumnIndex(People.NAME); String name = cur.getString(nameColumnIndex);
//let’s now see how cwn loop through a cursor
Trang 25@Override public boolean onCreate() {
//TODO: Construct the underlying database return true;
} }
Trang 265 CONTENT PROVIDER
• Truy xu t thông tin (thông qua bi n có tên uriMatcher)
private class final int ALLROWS = 1;
private class final int SINGLE_ROWS = 2;
private static final UriMatcher uriMatcher;
{
/*Populate the UriMatcher object, where a URI ending in
‘items’ will correspond to a request for all items, and
‘items/[rowID]’ represents a single row */
static
{
uriMatcher = new UriMatcher (UriMatcher.NO_MATCH);
uriMatcher.addURI (“com.paad.provider.myApp”, “items”, ALLROWS);
uriMatcher.addURI (“com.paad.provider.myApp”,“items/#”,SINGLE_ROW); }
Trang 275 CONTENT PROVIDER
• S d ng UriMatcher
Trang 285 CONTENT PROVIDER
• Truy v n trong provider
Trang 295 CONTENT PROVIDER
• S d ng đ i t ng ContentResolver đ truy xu t vào
provider thông qua ph ng th c getContentResolver().
Trang 305 CONTENT PROVIDER
• Insert Uri vào provider
Trang 315 CONTENT PROVIDER
• Insert d li u vào provider
Trang 325 CONTENT PROVIDER
• Delete Uri trong provider
• Delete d li u
Trang 335 CONTENT PROVIDER
• Update Uri trong provider
• Update d li u
Trang 355 CONTENT PROVIDER
String uriString = "content://contacts/people/";
Cursor myCursor = managedQuery(Uri.parse(uriString),
null, null, null);
String[] fromColumns = new String[] {People.NUMBER,
People.NAME}; int[] toLayoutIDs = new int[] {R.id.numberTextView,
R.id.nameTextView}; SimpleCursorAdapter myAdapter;
myAdapter = new SimpleCursorAdapter(this,
R.layout.simplecursorlayout, myCursor,
fromColumns, toLayoutIDs); myListView.setAdapter(myAdapter);