5 Saving Data Anuchit Chalothorn anoochit@gmail.comLicensed under a Creative Commons Attribution-NonCommercial-ShareAlike ...
Data StorageAndroid provides several options for you tosave persistent application data.● Share Preference● Internal stora...
Shared PreferenceThe SharedPreferences class provides ageneral framework that allows you to save andretrieve persistent ke...
Setting ActivityApplications often include settings that allowusers to modify app features and behaviors.You should use An...
Workshop: Setting ActivityCreate Setting Activity to store username andpassword in shared preference data
Internal StorageAccess to the file system is performed via thestandard java.io classes. Android provides alsohelper classe...
Internal StorageFor example the getDir(String, int) methodwould create or access a directory. TheopenFileInput(String s) m...
Permission Modeint specifies the permissions which are:● MODE_PRIVATE  ○ No access for other applications● MODE_WORLD_READ...
Environment Variable●   DIRECTORY_ALARMS●   DIRECTORY_DCIM●   DIRECTORY_DOWNLOADS●   DIRECTORY_MOVIES●   DIRECTORY_MUSIC● ...
Workshop: Get your pathAndroid provide methods to get path fromexternal storage and internal storageFile internal_path,ext...
SQLiteSQLite is an Open Source Database which isembedded into Android. SQLite supportsstandard relational database feature...
Useful Tools● SQLite database browser● SQLite Manager
SQLite in AndroidSQLite is available on every Android device,does not require any database setup oradministration. You onl...
SQLite Open HelperTo create and upgrade a database in yourAndroid application you usually subclassSQLiteOpenHelper. In the...
Workshop: SQLite Open HelperCreate blank app with database helper classextend from SQLiteOpenHelper class, identifydatabas...
Workshop: SQLite External StorageCreate blank app with database helper classextend from SQLiteOpenHelper class, identifyda...
Workshop: CRUDCreate an app to store, select, update anddelete. Using following scenarios:● Insert 2 rows    ○ 1, Jim, 023...
Workshop: Insert DataUse insert() method to insert data in SQLitetable.ContentValues val = new ContentValues();val.put("na...
QueryQueries can be created via the rawQuery() andquery() methods or via the SQLiteQueryBuilderclass .● rawQuery() directl...
Query ExampleMethod rawQuery() ExampleCursor cursor = getReadableDatabase().  rawQuery("select * from todo where _id =?", ...
Query ExampleMethod query() Exampledatabase.query(DATABASE_TABLE,  new String[] { KEY_ROWID, KEY_CATEGORY,KEY_SUMMARY, KEY...
CursorA query returns a Cursor object. A Cursorrepresents the result of a query and basicallypoints to one row of the quer...
CursorTo move between individual data rows, you canuse the moveToFirst() and moveToNext()methods. The isAfterLast() method...
Workshop: SelectUse db.query to query your data and return intoArray.
Workshop: Select AllTwo ways to return select all records dependon return data; Array and ArrayList. Use db.rawQuery to qu...
Workshop: UpdateUse a db.update() method to update data inspecific record and criteria.
Workshop: DeleteUse a db.delete() method to delete data inspecific record and criteria.
Put it together in real life!You may pre load data in the app or try toupdate data from internet, so the easy way putit in...
Push data to a ListViewQuery data and return to an ArrayList thenpush ArrayList to ListView.
End
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Android App Development 05 : Saving Data
Upcoming SlideShare
Loading in...5
×

Android App Development 05 : Saving Data

1,811

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,811
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
122
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Android App Development 05 : Saving Data

  1. 1. 5 Saving Data Anuchit Chalothorn anoochit@gmail.comLicensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  2. 2. Data StorageAndroid provides several options for you tosave persistent application data.● Share Preference● Internal storage● External Storage● SQLite Database● Network Connection
  3. 3. Shared PreferenceThe SharedPreferences class provides ageneral framework that allows you to save andretrieve persistent key-value pairs of primitivedata types.
  4. 4. Setting ActivityApplications often include settings that allowusers to modify app features and behaviors.You should use Androids Preference APIs tobuild an interface thats consistent with the userexperience in other Android apps (including thesystem settings).Ref: http://developer.android.com/guide/topics/ui/settings.html
  5. 5. Workshop: Setting ActivityCreate Setting Activity to store username andpassword in shared preference data
  6. 6. Internal StorageAccess to the file system is performed via thestandard java.io classes. Android provides alsohelper classes for creating and accessing newfiles and directories.
  7. 7. Internal StorageFor example the getDir(String, int) methodwould create or access a directory. TheopenFileInput(String s) method would open afile for input and openFileOutput(String s, int)would create a file.
  8. 8. Permission Modeint specifies the permissions which are:● MODE_PRIVATE ○ No access for other applications● MODE_WORLD_READABLE ○ Read access for other applications● MODE_WORLD_WRITABLE ○ Write access for other applications● MODE_WORLD_READABLE | MODE_WORLD_WRITABLE ○ Read / Write access
  9. 9. Environment Variable● DIRECTORY_ALARMS● DIRECTORY_DCIM● DIRECTORY_DOWNLOADS● DIRECTORY_MOVIES● DIRECTORY_MUSIC● DIRECTORY_NOTIFICATIONS● DIRECTORY_PICTURES● DIRECTORY_PODCASTS● DIRECTORY_RINGTONES
  10. 10. Workshop: Get your pathAndroid provide methods to get path fromexternal storage and internal storageFile internal_path,external_path;internal_path=getDir(Environment.DIRECTORY_DOWNLOADS, 0);external_path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
  11. 11. SQLiteSQLite is an Open Source Database which isembedded into Android. SQLite supportsstandard relational database features like SQLsyntax, transactions and prepared statements.
  12. 12. Useful Tools● SQLite database browser● SQLite Manager
  13. 13. SQLite in AndroidSQLite is available on every Android device,does not require any database setup oradministration. You only have to define theSQL statements for creating and updating thedatabase.
  14. 14. SQLite Open HelperTo create and upgrade a database in yourAndroid application you usually subclassSQLiteOpenHelper. In the constructor of yoursubclass you call the super() method ofSQLiteOpenHelper, specifying the databasename and the current database version.
  15. 15. Workshop: SQLite Open HelperCreate blank app with database helper classextend from SQLiteOpenHelper class, identifydatabase name, version number and createtable at onCreate method. Call database helperfrom main activity. Your database will create at/data/data/packagename/files/databasename
  16. 16. Workshop: SQLite External StorageCreate blank app with database helper classextend from SQLiteOpenHelper class, identifydatabase name which store in external storagefiles directory, version number and create tableat onCreate method. Call database helper frommain activity. Your database will create at/sdcard/Android/data/packagename/files/databasename.db
  17. 17. Workshop: CRUDCreate an app to store, select, update anddelete. Using following scenarios:● Insert 2 rows ○ 1, Jim, 023645456 ○ 2, John, 023645876● Select John● Select all records● Update Jims number to 024953658● Delete John
  18. 18. Workshop: Insert DataUse insert() method to insert data in SQLitetable.ContentValues val = new ContentValues();val.put("name", name);val.put("tel", tel);long row = db.insert("member", null, val);
  19. 19. QueryQueries can be created via the rawQuery() andquery() methods or via the SQLiteQueryBuilderclass .● rawQuery() directly accepts an SQL select statement as input.● query() provides a structured interface for specifying the SQL query.
  20. 20. Query ExampleMethod rawQuery() ExampleCursor cursor = getReadableDatabase(). rawQuery("select * from todo where _id =?", new String[] {id});
  21. 21. Query ExampleMethod query() Exampledatabase.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_CATEGORY,KEY_SUMMARY, KEY_DESCRIPTION }, null, null, null, null, null);
  22. 22. CursorA query returns a Cursor object. A Cursorrepresents the result of a query and basicallypoints to one row of the query result. This wayAndroid can buffer the query results efficiently;as it does not have to load all data intomemory.
  23. 23. CursorTo move between individual data rows, you canuse the moveToFirst() and moveToNext()methods. The isAfterLast() method allows tocheck if the end of the query result has beenreached.
  24. 24. Workshop: SelectUse db.query to query your data and return intoArray.
  25. 25. Workshop: Select AllTwo ways to return select all records dependon return data; Array and ArrayList. Use db.rawQuery to query your data.
  26. 26. Workshop: UpdateUse a db.update() method to update data inspecific record and criteria.
  27. 27. Workshop: DeleteUse a db.delete() method to delete data inspecific record and criteria.
  28. 28. Put it together in real life!You may pre load data in the app or try toupdate data from internet, so the easy way putit in asset and copy to another storage.
  29. 29. Push data to a ListViewQuery data and return to an ArrayList thenpush ArrayList to ListView.
  30. 30. End
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×