0% found this document useful (0 votes)
3 views

Chapter 5 - Android Data Management

The document provides an overview of data management in Android, covering various methods such as Preferences, File I/O, XML files, SQLite databases, and Content Providers. It explains how to store and access data, including the use of key-value pairs for preferences and the structure of SQLite databases. The conclusion emphasizes choosing the appropriate data storage method based on the type and amount of data required.

Uploaded by

merir143
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter 5 - Android Data Management

The document provides an overview of data management in Android, covering various methods such as Preferences, File I/O, XML files, SQLite databases, and Content Providers. It explains how to store and access data, including the use of key-value pairs for preferences and the structure of SQLite databases. The conclusion emphasizes choosing the appropriate data storage method based on the type and amount of data required.

Uploaded by

merir143
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 23

Android

Data management

Chapter 5
Data: outline

Data Management in Android


Preferences
Text Files
XML Files
SQLite Database
Content Provider
Managing Data

Preferences: Key/Value pairs of data

Direct File I/O: Read/write files onboard or on SD cards. Remember to req uest
permission for writing, for instance, on SD card

Database Tables: SQL Lite

Application Direct Access: Read only access from res assets/raw directories

Increase functionality:
Content Providers: expose data to other applications
Services: background processes that run detached from any view
Preference system

Preferences are a convenient way to store


configuration parameters
Structured with a key-value mode

Preference TAG Preference VALUE

Preference TAG Preference VALUE


Preferences
Preference TAG Preference VALUE

Preference TAG Preference VALUE


Preferences types

Preferences could be either private or shared


– Shared means that other applications could
potentially read such preferences
– Private means that they could be restricted at
• Application level
• Activity level
Preferences types

Shared preferences
getSharedPreferences(String name, Context.MODE_WORLD_READABLE);
getSharedPreferences(String name, Context.MODE_WORLD_WRITABLE);

Private at application level


getSharedPreferences(String name, Context.MODE_PRIVATE);
Private at activity level
getPreferences(int mode);
Preferences editor

How to edit preferences?


You need to a SharedPreferences.Editor

SharedPreferences.Editor editor = pref.edit();


editor.putString("mydata", et.getText().toString());
editor.commit();

Be sure to commit operations at the end


Preferences screens

Could be defined via XML


Some specializations to ease the process
– CheckBoxPreference
– EditTextPreference
– ListPreference
– RingtonePreference
Create a class that extends PreferenceActivity and
call
addPreferencesFromResource(R.xml.mypreferences);
The Android FileSystem

Linux architecture
User privileges
– Quite limited
Onboard data
– Application's reserved data
External data
– SD card (/mnt/sdcard)
File I/O

Onboard
– Write to a designated place for each application
– Where? /data/data/<package>/files
– How? Use standard java I/O classes
SD card
– Where? Environment.getExternalStorageDirectory()
– How? Use standard java I/O classes
– Permissions? android.permission.WRITE_EXTERNAL_STORAGE
Raw Text Files: how?

Raw Text File


Place it under res/raw/ directory
Fill it with the text you like
Cannot edit it
Populate a TextView with it’s content inside the code
TextView tv = (TextView)findViewById(R.id.tv_main);
tv.setText(streamToString(R.raw.myfile));
XML Files: how?

XML File
Place it under res/xml/ directory
Start the file with
<?xml version=“1.0” encoding=“utf-8”?>
Add whatever you want with <mytag>value</mytag>
XML Files: example

We want to visualize all the grades of this class


Our XML file is like this:
<student
name=“Student’s name”
class=“Laboratorio di Applicazioni Mobili”
year=“2012”
grade=“30L” />
Cursors: data handlers

A Cursor stores data given by a DB query


Some methods:
getCount()
moveTo{First,Next,Last,Position,Previous}()
close()
You need to look inside the Cursor to see query’s
results
while (gradeCursor.moveToNext()) {
Log.v(“GRADES”,gradeCursor.getString(0));
}
Cursors: methods

Manipulating the cursor


– cursor.moveToFirst()
– while (cursor.moveToNext())
– for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
Get column numbers from names
– int nameColumn = cursor.getColumnIndex(People.NAME);
– int phoneColumn = cursor.getColumnIndex(People.NUMBER);
Get Data from column
– String name = cursor.getString(nameColumn);
– String number = cursor.getString(phoneColumn);
Cursor methods

• Manipulate the cursor (row pointer)


– cursor.moveToFirst()
– while (cursor.moveToNext()) { /* code */ }
– for (cursor.moveToFirst(); !cursor.isAfterLast(); cur.moveToNext) { /* code
*/ }
• Get column numbers from names
– int nameColumn = cursor.getColumnIndex(People.NAME);
– int phoneColumn = cursor.getColumnIndex(People.NUMBER);
• Get Data from column
– String name = cursor.getString(nameColumn);
– String number = cursor.getString(phoneColumn);
Content Providers

A system to access shared data


Similar to a REST web service
To each Content Provider, one or more URIs are
assigned in the form:
content://<authority>/path
To build a Content Provider

Define the DB
Create a class that extends
android.content.ContentProvider
Implement query(), insert(), update(), delete()
Register the ContentProvider in the manifest
How to use a Content Provider

Need to get the URI


– Usually this is declared as public inside the content
provider class
Make a query, maybe adding some where clauses
– You'll get a Cursor after that
Navigate the Cursor
Content Providers

Content
Provider
Example: contacts

Query the contacts content provider


Contacts information are shared among applications
You need to request a permission

<uses-permission android:name="android.permission.READ_CONTACTS"/>
Saving Data in Files in android

In Android, you can save data in files using several


methods. Here are the most common approaches:

1. Internal Storage
You can save files to the internal storage of the
device, which is private to your application.

2. External Storage
You can also save files to external storage, which
can be accessed by other applications.
Saving Data, Cont.….

3. Shared Preferences
For saving small amounts of data, such as user preferences,
you can use Shared Preferences.
4. Using SQLite Database
For structured data that requires complex queries, you might
consider using an SQLite database.
Conclusion
Choose the method that best fits your needs based on the type and
amount of data you want to store. For simple key-value pairs, Shared
Preferences are sufficient; for larger files, consider internal or external
storage; and for structured data, an SQLite database is appropriate.

You might also like