Reading from preferences
Reading from prefs is pretty straightforward. Once you get ahold of a preference file,
you can use aptly named functions to read the variables of the data types mentioned
earlier in this chapter. You can use functions such as
getInt()
,
getString()
and
getLong()
, passing in a key and an optional default value to get the stored value. An
example call, to get a
String
, by the key
username
, with a default value of an empty
String
is as follows:
preferences.getString("username", "")
.
You will learn more about reading data through the chapter's project, but for now,
let's see how to write some data to
SharedPreferences
.
Saving Data on Android
Chapter 2: Shared Preferences
raywenderlich.com
42
Do'stlaringiz bilan baham: |