Transcript Slide 1

CHAPTER 9
File Storage
Shared Preferences
SQLite
Chapter objectives:
•
•
•
•
•
Data storage methods
Understand what Shared Preferences are used for
Key-value pairs
File-based Storage
The differences between internal storage and
external storage
• How to build applications using SQLite databases
9.1 Storing Data
• Applications often store information about a
user’s preferences in order to provide a more
sophisicated level of personalization and
responsiveness
• A user can log into and out of an application and
have it remember them when the application is
relaunched
• Users expect to choose settings according to their
personal needs, such as specific background
sounds and images, and have them automatically
persist across user sessions
• Advanced applications are frequently datadriven and require the management of a
larger volume of data
• Databases and files can be used by an
application to store structured data or
information that will be made available to
other applications
• Choosing the manner in which data is stored
depends upon the specific needs of the
application, such as the amount of data that
needs to be stored and whether the data will
be kept private
• The Android platform allows data files to be
saved on the device’s internal memory and
on an external storage media
• Files that are saved within the device’s
internal storage memory tend to be small, as
opposed to external storage, which typically
holds larger volume files
9.2 Shared Preferences
• Shared Preferences refers to the storage of a
limited set of primitive data used to make
persistent changes in an Android application
• A simple way to read and write key-value pairs of
data
• Used to store a user’s personal settings and a
small amount of application session data
• SharedPreference information is backed up in
XML files in internal storage
• Proprietary and inaccessible to outside
applications
• Allows preference data values to
automatically persist throughout
sessions, regardless of how they end
• This includes exit of an application,
shutdown of the device, or system crash
• A key-value pair is a data representation
model that uses a set of key identifiers along
with associated data values
• Model is frequently used in hash tables and
configuration files
• Provides access to a data value or object by
specifying its associated key
• SharedPreferences API provides a set of
methods for reading and writing key-value
pairs to files
• Shared preferences supports the String data
type and Java primitive data types, such as int,
float, long, and boolean
9.3 File Storage – Internal and External
Storage
• In Android, a file-based storage option allows
data to be written to an actual file structure
• This storage method requires more control
regarding read and write permissions
• Internal storage allows data to be stored directly
onto the device’s memory
• This storage is always available, assuming there is
space
• External storage may not always be obtainable on
a device.
• There are significant differences in how
external and internal storage is utilized in an
application
• Internal storage files can be configured to be
readable and writeable by the application
• Typically, internal storage is utilized when
processing an image, video and audio
elements, and large data files
• By default, files saved to internal storage are
private to the application
• External storage is publicly shared storage,
which means that it can be made available
to external applications
• Unlike internal storage, once an
application is uninstalled, external storage
files will continue to exist
9.4 Android Database with SQLite
• SQLite provides the foundation for managing
private and embedded databases in an Android
application
• The Android SDK includes the SQLite software
library that implements the SQL (Structured
Query Language) database engine
• This library is used for defining the database
structure and adding and managing the database
content as required by the application
• The Android SDK also includes an SQLite database
tool for explicit database debugging purposes
• As a condensed version of SQL
(Structured Query Language), SQLite
supports the standard SQL syntax and
database transactions
• Though SQLite is not a full-featured
database, it supports a large set of the
SQL standard and is sufficient for Android
developers needing a simple database
engine to plug into their applications
• An SQLite database file is a collection of data
organized in a table
• The SQLite database table is structured by a
set of rows and columns
• A row represents a single record, the
implicitly structured data entry in the table
• A column represents a data field, an attribute
of a data record
• A field is a single item that exists at the
intersection between one row and one
column
• The possible data types for an individual
field of data consist of NULL, INTEGER,
REAL, TEXT, and BLOB
• BLOB is a data type used to store a large
array of binary data (bytes)
• SQLite does not provide specific data
storage classes for values that need to be
represented as Boolean, date, or time.
Instead, these values can be stored as an
INTEGER or TEXT
An outline for a students database
table schema
9.5 SQLiteOpenHelper
• The Android SDK provides a set of classes for
working with SQLite databases
• SQLiteOpenHelper is essential for the
creation and management of database
content, as well as database versioning
• In an Android SQLite application,
SQLiteOpenHelper must be subclassed
• It must contain the implementation of
onCreate() and onUpgrade()
9.6 Adapters and AdapterViews
• An Adapter object is a mechanism that binds a
data source, such as a database, to an
AdapterView
• An AdapterView is a container widget that is
populated by a data source, determined by an
Adapter
• Common AdapterViews are ListViews,
GridViews, and Spinners
• A ListView is an AdapterView that provides
a simple approach for an application to
display a scrolling list of records
• These records can be displayed with a
default format, using a built-in style, or
customized extensively
• As an AdapterView, a ListView object
requires an Adapter to provide it with data
• A ListView consists of a collection of
sequential items.