Presentation is loading. Please wait.

Presentation is loading. Please wait.

 data/data-storage.html#pref data/data-storage.html#pref 

Similar presentations

Presentation on theme: " data/data-storage.html#pref data/data-storage.html#pref "— Presentation transcript:


2  data/data-storage.html#pref data/data-storage.html#pref  ples/63.html ples/63.html  Hello Android, chapter 9

3  Shared Preferences Shared Preferences ◦ Store private primitive data in key-value pairs.  Internal Storage Internal Storage ◦ Store private data on the device memory.  External Storage External Storage ◦ Store public data on the shared external storage.  SQLite Databases SQLite Databases ◦ Store structured data in a private database.  Network Connection Network Connection ◦ Store data on the web with your own network server

4 Store private primitive data in key-value pairs

5  How to save and retrieve data?  Using SharePrference class ◦ persistent key-value pairs ◦ only primitive data types.  booleans, floats, ints, longs, and strings.  This data will persist across user sessions ◦ even if your application is killed


7  getSharedPreferences() getSharedPreferences() ◦ Use this if you need multiple preferences files identified by name, which you specify with the first parameter.  getPreferences() getPreferences() ◦ Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name

8  Write ◦ Call edit() to get a SharedPreferences.Editor.edit()SharedPreferences.Editor ◦ Add values with methods such as putBoolean() and putString().putBoolean() putString() ◦ Commit the new values with commit()commit()  Read ◦ use SharedPreferences methods such as getBoolean() and getString().SharedPreferences getBoolean()getString()


10 Store private data on the device memory

11  You can save files directly on the device's internal storage.  By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user).  When the user uninstalls your application, these files are removed.

12  Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream.openFileOutput()FileOutputStream  Write to the file with write().write()  Close the stream with close().close()

13  Call openFileInput() and pass it the name of the file to read. This returns a FileInputStream.openFileInput()FileInputStream  Read bytes from the file with read().read()  Then close the stream with close().close()




17  XML resources can mean resources in general that are defined in XML, ◦ layout files, styles, arrays, res/xml XML files.  In this section we will be dealing with res/xml XML files.






23 SQLite Databases

24  A tiny yet powerful database engine created by Dr. Richard Hipp in 2000.  It is arguably the most widely deployed SQL database engine in the world.  Besides Android, SQLite can be found in the Apple iPhone, Symbian phones, Mozilla Firefox, Skype, PHP, Adobe AIR, Mac OS X, Solaris, and many other places.

25  It’s free. ◦ The authors have placed it in the public domain and don’t charge for its use.  It’s small. ◦ The current version is about 150KB, well within the memory budget of an Android phone.  It requires no setup or administration. ◦ There is no server, no config file, and no need for a database administrator.

26  Android stores the file ◦ /data/data/packagename/databases  You can take that file, move it around, and even copy it to another system ◦ for example, from your phone to your workstation), and it will work fine.  You can use the adb command or the File Explorer view in Eclipse to view, move, or delete it. ◦ Window > Show View > Other... > Android > File Explorer)

27 Nothing here yet

28  Goal ◦ Save time stamps and string to a table ◦ Display it

29  Constants describing the database ◦ Constants interface  represent the database ◦ Using SQLiteOpenHelper  Driver ◦ Main program


31 manages database creation and versions. All you need to do is provide a constructor and override two methods. DATABASE_VERSION is just a number we make up. If this were a real program, you would increase the version number whenever you had to make significant changes to the database design (for example, to add a new column).




35 close the database inside the finally block. So even if an error occurs in the middle, the database will still be closed. Every time you run this program, you’ll get a new event. Running a Query







42  Allows you to connect your model (data) to your view with just a few lines of code.  To demonstrate data binding, we’ll modify the Events example to use a ListView that is bound to the result of a database query.



45 putting the ID, time, and title on one line with colons in between the fields. added a little padding and formatting to make it look nice.

46 need to change the layout for the activity itself in layout/main.xml

Download ppt " data/data-storage.html#pref data/data-storage.html#pref "

Similar presentations

Ads by Google