Your SlideShare is downloading. ×
Android App Development 05 : Saving Data
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Android App Development 05 : Saving Data

1,730
views

Published on


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

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 5 Saving Data Anuchit Chalothorn anoochit@gmail.comLicensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  • 2. Data StorageAndroid provides several options for you tosave persistent application data.● Share Preference● Internal storage● External Storage● SQLite Database● Network Connection
  • 3. Shared PreferenceThe SharedPreferences class provides ageneral framework that allows you to save andretrieve persistent key-value pairs of primitivedata types.
  • 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. Workshop: Setting ActivityCreate Setting Activity to store username andpassword in shared preference data
  • 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. 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. 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. Environment Variable● DIRECTORY_ALARMS● DIRECTORY_DCIM● DIRECTORY_DOWNLOADS● DIRECTORY_MOVIES● DIRECTORY_MUSIC● DIRECTORY_NOTIFICATIONS● DIRECTORY_PICTURES● DIRECTORY_PODCASTS● DIRECTORY_RINGTONES
  • 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. SQLiteSQLite is an Open Source Database which isembedded into Android. SQLite supportsstandard relational database features like SQLsyntax, transactions and prepared statements.
  • 12. Useful Tools● SQLite database browser● SQLite Manager
  • 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. 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. 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. 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. 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. 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. 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. Query ExampleMethod rawQuery() ExampleCursor cursor = getReadableDatabase(). rawQuery("select * from todo where _id =?", new String[] {id});
  • 21. Query ExampleMethod query() Exampledatabase.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_CATEGORY,KEY_SUMMARY, KEY_DESCRIPTION }, null, null, null, null, null);
  • 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. 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. Workshop: SelectUse db.query to query your data and return intoArray.
  • 25. Workshop: Select AllTwo ways to return select all records dependon return data; Array and ArrayList. Use db.rawQuery to query your data.
  • 26. Workshop: UpdateUse a db.update() method to update data inspecific record and criteria.
  • 27. Workshop: DeleteUse a db.delete() method to delete data inspecific record and criteria.
  • 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. Push data to a ListViewQuery data and return to an ArrayList thenpush ArrayList to ListView.
  • 30. End