So, you have an Android app, and you need to store your data. The Android ‘Notepad‘ tutorial will show you the basics of SQLite, and I’ve modified their code quite a bit for my DataServices example project. The one thing I don’t like about their example is that the app itself knows that the data is stored in SQLite. While it is just an example project, I feel it’s setting a very bad example. So I’ve created a bit of abstraction and added an interface so that the SQLite implementation can be swapped in and out for any other storage service.
Below we’ll examine the IReminderDataService interface, which provides some basic data storage functionality. I’ve implemented two different storage mechanisms: visit SQLite and DB4O posts for specifics.
This interface is basic, a full-fledged interface would provide a bit more. We have Create, Update, Delete and FetchAll functions. You’ll notice that we’re still fully typed at this stage, ensuring that our App isn’t compromised by Casts or creating objects from a result row.
package com.hat6.dataServiceExample.model;
import java.util.List;
public interface IReminderDataService {
public abstract long createReminder(Reminder r);
public abstract boolean updateReminder(Reminder r);
public abstract boolean deleteReminder(Reminder r);
public abstract List<Reminder> fetchAllReminders();
}
This makes our application very streamlined. We simply instantiate an implementation, fetch all Reminders, then pass the ArrayList over to our ItemRenderers via the RenderedListAdapter (discussed here). It isn’t as generic as a full-on ORM, but it’s very lightweight.
/*... imports removed ...*/
public class DataServiceExample1 extends ListActivity {
private List<Reminder> reminders;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
IReminderDataService rds = new ReminderSQLiteImpl(this);
//createReminders(rds);
reminders = rds.fetchAllReminders();
RenderedListAdapter rla = new RenderedListAdapter(this, reminders, ReminderListSimpletemRenderer.class);
setListAdapter(rla);
}
private void createReminders(IReminderDataService rds){
// Add four items
Reminder r1 = new Reminder(
"Remember Once","today", "tomorrow", getResources().getDrawable(R.drawable.finger) );
Reminder r2 = new Reminder(
"Bell Reminder","today", "tomorrow", getResources().getDrawable(R.drawable.bell));
Reminder r3 = new Reminder(
"Laundry Reminder","today", "tomorrow", getResources().getDrawable(R.drawable.laundry));
Reminder r4 = new Reminder(
"Task Reminder","today", "tomorrow", getResources().getDrawable(R.drawable.task));
rds.createReminder(r1);
rds.createReminder(r2);
rds.createReminder(r3);
rds.createReminder(r4);
}
}
You can find the source code for the entire DataService project here: http://svn.hat6.com/hat6public/DataServiceExample/trunk
2 comments ↓
[...] ← Android DataServices [...]
[...] page dedicated to Android, though the source they provide won’t build. The code matches the DataService interface I provided earlier, so if you’ve read through the SQLite post, you should be able [...]