1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình android C10 luutrutruyxuatdulieutrongungdung

35 91 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 35
Dung lượng 831,59 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 1

L 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 3

1 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 5

1 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 6

String stringPreference=

sharedPre.getString (“textEntryValue”, “”);

Trang 7

2 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 8

2 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 9

3 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 10

3 B L U TR NGOÀI

• Ví d :

boolean mExternalStorageAvailable = false;

boolean mExternalStorageWriteable = false;

String state = Environment.getExternalStorageState();

Trang 11

4 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 13

4.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 14

4.3 M Database:

SQLitDatabase db;

try {

db=dbHelper.getWritableDatabase(); }

catch (SQLiteException ex) {

db = dbHelper.getReadableDatabase();

}

4 SQLite Database

Trang 15

4.4 M t s ph ng th c đ đi u khi n Cursor thao tác

Trang 16

return 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 17

4.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 18

4.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 19

4.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 20

4.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 23

Cursor

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 26

5 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 27

5 CONTENT PROVIDER

• S d ng UriMatcher

Trang 28

5 CONTENT PROVIDER

• Truy v n trong provider

Trang 29

5 CONTENT PROVIDER

S d ng đ i t ng ContentResolver đ truy xu t vào

provider thông qua ph ng th c getContentResolver().

Trang 30

5 CONTENT PROVIDER

• Insert Uri vào provider

Trang 31

5 CONTENT PROVIDER

• Insert d li u vào provider

Trang 32

5 CONTENT PROVIDER

• Delete Uri trong provider

• Delete d li u

Trang 33

5 CONTENT PROVIDER

• Update Uri trong provider

• Update d li u

Trang 35

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

Ngày đăng: 21/12/2017, 18:59

TỪ KHÓA LIÊN QUAN

w