Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Android App Development 05 : Saving Data


Published on

  • Be the first to comment

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:
  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 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
  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