Topic 3 - Files and Databases
Topic 3 - Files and Databases
Mobile Applications
University of Valencia
1 Introduction
2 Files
3 Preferences
4 SQLite databases
5 References
Índice
1 Introduction
2 Files
3 Preferences
4 SQLite databases
5 References
Introduction
Introduction
Índice
1 Introduction
2 Files
3 Preferences
4 SQLite databases
5 References
In the directory res/raw we can have files and from the application we can
read them. Those files will be inside the apk file and can only be read (can
not be modified).
First we obtain an object of type Resources:
// getResource is a method declared in Context
// and Activity extends from Context
Resources r = getResources();
Índice
2 Files
Internal storage
External storage
Internal storage
Internal storage
You can create files in internal storage as long as they are small files.
These files are private to the application and other applications do not have
access to them. When the user deletes the application, the files are also
deleted.
Internal storage directories can be obtained by calling the following
methods:
For example:
String file = ...;
FileOutputStream out = openFileOutput(fichero, Context.MODE_PRIVATE);
// Write information
out.close();
Índice
2 Files
Internal storage
External storage
External storage
Information that has a standard format (PDF, PNG, TXT, JPG, etc).
This information can be viewed/processed with other applications.
Therefore, it should not be deleted when the application is uninstalled.
Information that has a format known by the application. This is
information can not be viewed/processed with other applications.
Therefore, it should be deleted when the application is unistalled.
The following example obtains the default public directory used to store
music:
File f = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC);
Índice
1 Introduction
2 Files
3 Preferences
4 SQLite databases
5 References
Preferences
Preferences
Writing example:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
// Use the editor to put key/value pairs
editor.putLong ("interval", 3600);
// Write the values to file
editor.commit();
http://developer.android.com/reference/android/content/
SharedPreferences.Editor.html
Reading example:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
long default = 1800;
long interval = sharedPref.getLong("interval", default);
http://developer.android.com/reference/android/content/
SharedPreferences.html
Topic 3, Files and Databases 21/49
SQLite databases
Índice
1 Introduction
2 Files
3 Preferences
4 SQLite databases
5 References
Relational databases
Type Description
NULL The value is null
INTEGER Integer value
REAL Decimal value
TEXT Text
BLOB For binary data
Information about the name of the database, name of the table and
columns:
public class TasksDataBase extends SQLiteOpenHelper{
// Database name
private static final String DATABASE_NAME="Tasks";
Queries vs modifications
SELECT queries
Structure of queries:
select attr1,...,attrN from table1,...,tableX
where (restrictions)
group by attr_i, ..., attr_j
having (condition)
order by attr_k,...,attr_r
String sql = "select count(*) as total from table where date>? and
score>? ";
String[] args = {’01/01/2013’,’400’}
Cursor c = db.rawQuery(sql,args);
return cursor;
}
...
}
Example:
String table=TScores""
String [] columns = {"date", "name", "score"};
String[] seleccionArgs={"07/12/2017"};
String orderby="date";
Modification sentences
Modification sentences
// The second argument sets the name(s) of the column that can be null
// if we do not give the value for it (in the third argument)
// If we do not use this functionality, we pass null as the second argument
db.insert(TABLE_NAME,null,values);
}
...
}
Índice
4 SQLite databases
Use of the class that provides access to the database
Create an custom adapter using the Cursor
// Adapter that obtains and shows the data from a Cursor. Arguments:
// 1- The context
// 2- The layout of each item
// 3- The cursor that contains the data to be displayed
// 4- An array of String with the names of the columns to extract data
// 5- An array of int with the id of the views where the
@Override
protected void onDestroy() {
super.onDestroy ();
dbReg.close();
}
}
If our database access class provides methods that modify the data then we
must inform the list that the data has changed.
One possibility is to obtain the Cursor again and set it to the adapter:
// Obtain data (from an Activity, from an HTTP call, etc)
// Create a JavaBean with that information
// Pass that JavaBean to a method of our database class
Todo d = new Todo(...);
refBD.insertTodo(d);
Índice
4 SQLite databases
Use of the class that provides access to the database
Create an custom adapter using the Cursor
Custom adapter
One of the columns in the table is the path where an image is stored
(for instance in the SD card). We want to show this information not
as a String but as an ImageView.
One of the columns in the table is an integer that can have two
values: 0 or 1, thus representing a boolean. We want to show this
information as a CheckBox.
Adapter that creates the views from the data in the Cursor I
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater li = (LayoutInflater)context.getSystemService(Service.
LAYOUT_INFLATER_SERVICE);
View view = li.inflate(R.layout.item_registro_layout, null);
fillView(view, cursor);
return view;
Adapter that creates the views from the data in the Cursor II
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
fillView(view, cursor);
}
Adapter that creates the views from the data in the Cursor III
}else{
// If it is null obtain the views and assign them to the ViewHolder
tvTodo = (TextView)view.findViewById(R.id.textView1);
...
ViewHolder holder = new ViewHolder();
holder.tvTodo = tvTodo;
...
view.setTag (holder);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
...
refBD = new TasksDataBase(getApplicationContext(), version);
Cursor c = refBD.getAllData();
cursorAdapter = new TaskCursorAdapter(getApplicationContext(), c);
setListAdapter(cursorAdapter);
...
}
...
}
Índice
1 Introduction
2 Files
3 Preferences
4 SQLite databases
5 References
References
http://developer.android.com/training/basics/data-storage/index.html