Lập trình android C10 luutrutruyxuatdulieutrongungdung

35 90 0
Lập trình android C10 luutrutruyxuatdulieutrongungdung

Đ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

Ch ng 10 L U TR VÀ TRUY XU T D LI U TRONG NG D NG Lê V n H nh levanhanhvn@gmail.com N I DUNG 1.Shared Preferences B l u tr B l u tr SQLite Databases Content Provider 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 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 ph ng th c: ng l p SharedPreference thơng qua GetsharedPreferences(string, int) SharedPreferences • T ođ it ng SharedPreferences: SharedPreferences sharedPre = getSharedPreferences(PREFS_NAME, mode); Trong đó: o PREFS_NAME: tên c a sharedPreference c n t o o Mode: ki u ghi d li u     MODE_PRIVATE: mode ghi m c đ nh, ch có nh t ng d ng t o t p tin đ c phép truy c p vào MODE_WORLD_READABLE: cho phép ng d ng khác truy c p vào MODE_WORLD_WRITEABLE: cho phép ng d ng khác truy c p s a đ i MODE_MULTI_PROCESS: cho phép nhi u ti n trình ng d ng truy xu t vào m t th i m SharedPreferences • B sung thơng tin cho đ i t thông qua đ i t ng Editor ng SharedPreferences(): Ví d : SharedPreferences.Editor editor = sharedPre.edit(); editor.putBoolean(“isTrue”, true); editor.putFloat(“lastFloat”, 1f); editor.putInt(“wholeNumber”, 2); editor.putLong(“aNumber”, 3l); editor.putString(“textEntryValue”, “Not Empty”); //L u SharedPreference editor.commit(); SharedPreferences • L y d li u SharedPreferences(): s d ng hàm get tùy theo t ng lo i d li u Ví d : boolean isTrue = sharedPre.getBoolean(“isTrue”, false); float lastFloat = sharedPre.getFloat(“lastFloat”, 0f); int wholeNumber = sharedPre.getInt(“wholeNumber”, 1); long aNumber = sharedPre.getLong(“aNumber”, 0); String stringPreference= sharedPre.getString(“textEntryValue”, “”); B L U TR TRONG • L u d li u riêng b nh thi t b • Khi ng d ng đ c cài đ t, h th ng cung c p vùng b nh 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 s đ c xóa • T o l u tr file vào b l u tr o o o • G i openFileOutput() v i tên file ch đ truy c p file Ghi lên file s d ng ph ng th c write() óng stream v i ph ng th c close() c file t b l u tr o o o G i openFileInput() v i tên file c n đ c c n i d ng t file s d ng ph ng th c read() óng stream v i ph ng th c close() 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(); B L U TR NGỒI • L u tr d li u public b nh ngồi • M i thi t b Android có th h tr b l u tr ngồi s d ng l u file (nh SDcard) • D li u đ c l u tr b l u tr ngồi có th đ c thay đ i b i ng i dùng, có th đ ng b v i máy tính cá nhân • Tr c làm vi c v i b l u tr c n ki m tra tr ng thái c a b l u tr B L U TR NGỒI • Ví d : boolean mExternalStorageAvailable = false; boolean mExternalStorageWriteable = false; String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media mExternalStorageAvailable=mExternalStorageWriteable= true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { /* Something else is wrong It may be one of many other states, but all we need to know is we can neither read nor write*/ mExternalStorageAvailable=mExternalStorageWriteable=false; } CONTENT PROVIDER • Là n i đóng gói d li u cho phép thành ph n ng d ng ho c ng d ng khác truy c p s d ng • Android cung c p m t s gói content provider dùng cho vi c l u tr chung: • Browser • CallLog • Contact (people, phones, photos, group) • MediaStore (music, video, image) • Setting CONTENT PROVIDER • Mơ hình d li u theo đ nh d ng c t dòng gi ng nh c s d li u V i m i c t tr ng d li u m i dòng m t record • Cách th c ho t đ ng gi ng giao th c REST web • S d ng chu i URI đ th c hi n vi c rút trích c ng nh l u d li u content provider Ví d : content://com.android.book.BookProvider content://com.android.book.BookProvider/7 • C n khai báo file AndroidManifest.xml cho bi t ng d ng s d ng nh ng content provider CONTENT PROVIDER • S d ng Cursor đ th c hi n thao b ng d li u c a provider Cursor • M t s ph d li u: ng th c đ u n Cursor thao tác movetoFisrt movetoPrevious movetoNext movetoPosition getCount getColumnName getPosititon getColumnIndexOrThrow getColumnNames CONTENT PROVIDER • Ví d : if (cur.moveToFirst()==false) { // no rows empty cursor return; } //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 while (cur.moveToNext()) { // cursor moved suceessfully //access fields } CONTENT PROVIDER • T o m t Content Provider public class MyProvider extends ContentProvider { private static final String myURI = ”content://com.paad.provider.myapp/items”; public static final Uri CONTENT_URI = Uri.parse(myURI); @Override public boolean onCreate() { //TODO: Construct the underlying database return true; } } 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); } CONTENT PROVIDER • S d ng UriMatcher CONTENT PROVIDER • Truy v n provider CONTENT PROVIDER • S d ng đ i t ng ContentResolver đ truy xu t vào provider thông qua ph ng th c getContentResolver() CONTENT PROVIDER • Insert Uri vào provider CONTENT PROVIDER • Insert d li u vào provider CONTENT PROVIDER • Delete Uri provider • Delete d li u CONTENT PROVIDER • Update Uri provider • Update d li u CONTENT PROVIDER • S d ng SimpleCursorAdapter o Cho phép g n k t m t Cursor đ n ListView o S d ng layout t đ nh ngh a đ hi n th item • t o m t SimpleCursorAdapter c n: o Context hi n dùng o Layout o Cursor o M t array ch a tên c a c t o M t array ch a ID c a resource cho View hi n th 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ài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan