Categories
Studio

Room Persistence Library: Insert, Save, Delete

Previously, we got to know how to setup and implement Room library into our project. In this article, we’ll get to know how to insert, save and delete objects or populate or SQLite tables.

In MainActivity, I’ll first define the app database.

private AppDatabase appDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

appDatabase = AppDatabase.getDatabase(this);
}

Insert

First we will learn to insert the data into our database. Since our app is an alarm app the lifecycle goes like this: Click on AddAlarm button > Open TimePicker > (Select time and save) > Get Time in activity and save.

Room doesn’t allow you to do database operations on UI-thread so you need to create AsyncTask functions. So here’s our Insert AsyncTask Insert function.

private class insertTime extends AsyncTask<Alarms, Void, Void>
{
Alarms singleAlarm;

@Override
protected Void doInBackground(Alarms... alarms) {
singleAlarm = alarms[0];
appDatabase.myDao().insert(singleAlarm);
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
alarmsList.add(singleAlarm);
adapter.notifyItemInserted(adapter.getItemCount() - 1);

}
}

Here’s how we are going to call this function to save the alarm.

Alarms singleAlarm = new Alarms("AlarmName", boolean, intHour, intMinute);
new insertTime().execute(singleAlarm);

Show (Query)

Before moving on to the next part, run the app and check for errors. Now move on to the next part. Let’s show our saved queries. Create another AsyncTask function in MainActivity. Here’s its code:

private class getAlarms extends AsyncTask<Void, Void, Void>
{

@Override
protected Void doInBackground(Void... voids) {
alarmsList = (ArrayList<Alarms>) appDatabase.myDao().getAlarms();
return null;

}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);

adapter = new AlarmAdapter(alarmsList);
recyclerView.setAdapter(adapter);
}
}

And this is how we are going to call it from MainActivity’s OnCreate()

new getAlarms().execute();

Delete

After learning to add data and query, we’ll proceed to implement Delete functionality of alarms. For this, we will use the swipe to delete functionality. Here’s the asynctask function:

private class deleteAlarm extends AsyncTask<Alarms, Void, Void>
{
int pos;
public deleteAlarm(int pos) {
this.pos = pos;
}

@Override
protected Void doInBackground(Alarms... alarms) {
appDatabase.myDao().deleteAlarm(alarms[0].getId());
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
alarmsList.remove(pos);
adapter.notifyItemRemoved(pos);
}
}

And then we will call this function to delete it from database as well as remove from the adapter. Below is my code for Swipe to delete & Delete+Remove.

// Swipe to delete.
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT)
{

@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
return false;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
// Item swiped.
int pos = viewHolder.getAdapterPosition();

// Remove from database
new deleteAlarm(pos).execute(alarmsList.get(pos));
}
};

// attach it to the recyclerview
new ItemTouchHelper(simpleCallback).attachToRecyclerView(recyclerView);

Leave a Reply

Your email address will not be published. Required fields are marked *