Storing and retrieving data

Appportunity

Build your own Android App Dev Empire

Get Instant Access

This chapter covers:

■ Storing and retrieving data with SharedPreferences

■ Using the filesystem

■ Working with a SQLite database

■ Accessing and building a ContentProvider

Anytime you are developing software, one of the most common and basic constructs you have to deal with is the means to store and retrieve data. It's all about the data after all. Though there are many ways to pipe data into and out of various languages and technologies, there are typically only a few ways to persist it: in memory structures, the filesystem, databases, and network services.

Like other technologies, Android has its own concepts for getting and sharing data in applications, yet these concepts are ultimately implemented using familiar approaches (for the most part). Android provides access to the filesystem, has support for a local relational database through SQLite, and includes a Shared-Preferences object and preferences system that allows you to store simple key-value pairs within applications.

In this chapter we are going to take a tour of each of the local data-related mechanisms (we will examine the network possibilities in chapter 6). We will start with preferences and create a small sample application to exercise those concepts. From there we will create another sample application to examine using the filesystem to store data, both internal to our application and external using the platform's SD card support. Then we will look at creating and accessing a database. To do this we will take a closer look at some of the code and concepts from the WeatherReporter application we created in chapter 4, which uses SQLite.

Beyond the basics, Android also includes its own construct that allows applications to share data through a clever URI-based approach called a ContentProvider. This technique combines several other Android concepts, such as the URI-based style of intents and the Cursor result set seen in SQLite, to make data accessible across different applications. To demonstrate how this works we will create another small sample application that uses built-in providers, then we will walk through the steps required to create a ContentProvider on our own.

We begin with the easiest form of data storage and retrieval Android provides, preferences.

Was this article helpful?

+2 -4

Post a comment