Hi everyone, in this post we will learn about another architectural component of android which is Room Persistence Library. In this android room database example, we will learn how to use room for handling our SQLite database.
Here is a new Android Room Database Example using Kotlin.
This is a video tutorial series and throughout this we will build a basic notes application using Room persistence library.Â
Table of Contents
What is Room?
The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite.
Basically, with the help of room we can quickly create sqlite databases and perform the operations like create, read, update and delete. Room makes everything very easy and quick.
Components of Room
We have 3 components of room.
- Entity:Â Instead of creating the SQLite table, we will create the Entity. Entity is nothing but a model class annotated with @Entity. The variables of this class is our columns, and the class is our table.
- Database: It is an abstract class where we define all our entities.
- DAO: Stands for Data Access Object. It is an interface that defines all the operations that we need to perform in our database.
I will not go to further theoretical details of Room. But you can check this link for more details.
Android Room Database Example
So what we are going to do is, we are going to create a To Do application using SQLite and in this application we will learn how we can use Room for handling SQLite.
Creating a new Project
- So create a new project in Android Studio. I have named this project as My ToDo.
- The first thing that we will do is we will add all the required dependencies.
Adding Dependencies
- Come inside app level build.gradle file and add the following dependencies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
dependencies { //define versions def support_version = "27.0.2" def room_version = "1.1.1" implementation fileTree(include: ['*.jar'], dir: 'libs') implementation "com.android.support:appcompat-v7:$support_version" implementation 'com.android.support.constraint:constraint-layout:1.1.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' //add these libraries //support design implementation "com.android.support:design:$support_version" //card view implementation "com.android.support:cardview-v7:$support_version" //recyclerview implementation "com.android.support:recyclerview-v7:$support_version" //room implementation "android.arch.persistence.room:runtime:$room_version" annotationProcessor "android.arch.persistence.room:compiler:$room_version" testImplementation "android.arch.persistence.room:testing:$room_version" } |
- After adding the above dependencies sync your project and you are good to go.
Creating Activities
- We already have a MainActivity created, other than this MainActivity we need, AddTaskActivity and UpdateTaskActivity.
- So create 2 more activities and name them AddTaskActivity and UpdateTaskActivity.
MainActivity
- Before designing the activities define these colors in your colors.xml.
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#579937</color> <color name="colorPrimaryDark">#356f19</color> <color name="colorAccent">#0a7455</color> <color name="colorLight">#cff5bc</color> <color name="colorRed">#e70004</color> </resources> |
- Come inside activity_main.xml and add the following xml code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp" tools:context=".MainActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerview_tasks" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.FloatingActionButton android:id="@+id/floating_button_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_margin="8dp" android:backgroundTint="@color/colorPrimaryDark" android:src="@drawable/ic_add" android:tint="@color/colorLight" app:fabSize="normal" /> </RelativeLayout> |
- In our main activity we will display all the added task, and an add button at the bottom from where user can add a new task.
- For displaying all the added task we defined RecyclerView and FloatingActionButton for adding a new task.
AddTaskActivity
- From this activity we will add a new task to our application. So we need an interface like this.
- For designing the above screen write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".AddTaskActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:text="Add a Task" android:textAlignment="center" android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline" /> <EditText android:id="@+id/editTextTask" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="task?" /> <EditText android:id="@+id/editTextDesc" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="description..." /> <EditText android:id="@+id/editTextFinishBy" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="finish by?" /> <Button android:id="@+id/button_save" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:background="@color/colorPrimary" android:text="Save" android:textAllCaps="false" android:textColor="@color/colorLight" /> </LinearLayout> </RelativeLayout> |
UpdateTaskActivity
- Finally go inside activity_update_task.xml and write the following xml code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".UpdateTaskActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:text="Update Task" android:textAlignment="center" android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline" /> <EditText android:id="@+id/editTextTask" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="task?" /> <EditText android:id="@+id/editTextDesc" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="description..." /> <EditText android:id="@+id/editTextFinishBy" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="finish by?" /> <CheckBox android:id="@+id/checkBoxFinished" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Mark as finished" /> <Button android:id="@+id/button_update" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:background="@color/colorPrimary" android:text="Update" android:textAllCaps="false" android:textColor="@color/colorLight" /> <Button android:id="@+id/button_delete" android:layout_width="200dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:background="@color/colorRed" android:text="Delete" android:textAllCaps="false" android:textColor="@color/colorLight" /> </LinearLayout> </RelativeLayout> |
Tasks RecyclerView Layout
We will display all the tasks added in a RecyclerView, and for this we need one more layout file. So create a layout file and name it recyclerview_tasks.xml and write the following xml code inside.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.v7.widget.CardView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="3dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="7dp"> <TextView android:id="@+id/textViewStatus" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:background="@color/colorPrimaryDark" android:text="Completed" android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" android:textColor="@color/colorLight" android:textStyle="bold" /> <TextView android:id="@+id/textViewTask" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Go Bring Eggs" android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline" /> <TextView android:id="@+id/textViewDesc" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Bring 6 eggs from super market" android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" /> <TextView android:id="@+id/textViewFinishBy" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="5pm today" android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" /> </LinearLayout> </android.support.v7.widget.CardView> </RelativeLayout> |
Creating Entity
Now here come the main thing, for every table that we need we need to create an entity. And for this application we need a single entity.
- So create a class named Task.java and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package net.simplifiedcoding.mytodo; import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; import android.arch.persistence.room.PrimaryKey; import java.io.Serializable; @Entity public class Task implements Serializable { @PrimaryKey(autoGenerate = true) private int id; @ColumnInfo(name = "task") private String task; @ColumnInfo(name = "desc") private String desc; @ColumnInfo(name = "finish_by") private String finishBy; @ColumnInfo(name = "finished") private boolean finished; /* * Getters and Setters * */ public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTask() { return task; } public void setTask(String task) { this.task = task; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getFinishBy() { return finishBy; } public void setFinishBy(String finishBy) { this.finishBy = finishBy; } public boolean isFinished() { return finished; } public void setFinished(boolean finished) { this.finished = finished; } } |
- As you can see in the above code we have annotated the class with @Entity this is our table, and for the column id we have used @PrimaryKey(autoGenerate = true) this means this id will be auto increment, for other columns we used @ColumnInfo(name = “columnname”)
Creating Dao
- Now we need the Dao (Data Access Object). So create an interface named TaskDao.java and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
@Dao public interface TaskDao { @Query("SELECT * FROM task") List<Task> getAll(); @Insert void insert(Task task); @Delete void delete(Task task); @Update void update(Task task); } |
- You can see above we defined all the methods needed for the Create, Read, Update and Delete operation.
Creating Database
- Now create one more class and name it AppDatabase, and write the following code inside the class.
1 2 3 4 5 6 7 8 9 10 11 |
package net.simplifiedcoding.mytodo; import android.arch.persistence.room.Database; import android.arch.persistence.room.RoomDatabase; @Database(entities = {Task.class}, version = 1) public abstract class AppDatabase extends RoomDatabase { public abstract TaskDao taskDao(); } |
- In the above class we define all the entities and the database version.
Database Client
- Creating AppDatabase’s object is expensive so we will create a single instance of it.
- Create a class named DatabaseClient and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package net.simplifiedcoding.mytodo; import android.arch.persistence.room.Room; import android.content.Context; public class DatabaseClient { private Context mCtx; private static DatabaseClient mInstance; //our app database object private AppDatabase appDatabase; private DatabaseClient(Context mCtx) { this.mCtx = mCtx; //creating the app database with Room database builder //MyToDos is the name of the database appDatabase = Room.databaseBuilder(mCtx, AppDatabase.class, "MyToDos").build(); } public static synchronized DatabaseClient getInstance(Context mCtx) { if (mInstance == null) { mInstance = new DatabaseClient(mCtx); } return mInstance; } public AppDatabase getAppDatabase() { return appDatabase; } } |
Adding a Task
Now lets perform the create operation, which is adding a task to the database.
- Come inside AddTaskActivity and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
package net.simplifiedcoding.mytodo; import android.content.Intent; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class AddTaskActivity extends AppCompatActivity { private EditText editTextTask, editTextDesc, editTextFinishBy; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_task); editTextTask = findViewById(R.id.editTextTask); editTextDesc = findViewById(R.id.editTextDesc); editTextFinishBy = findViewById(R.id.editTextFinishBy); findViewById(R.id.button_save).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { saveTask(); } }); } private void saveTask() { final String sTask = editTextTask.getText().toString().trim(); final String sDesc = editTextDesc.getText().toString().trim(); final String sFinishBy = editTextFinishBy.getText().toString().trim(); if (sTask.isEmpty()) { editTextTask.setError("Task required"); editTextTask.requestFocus(); return; } if (sDesc.isEmpty()) { editTextDesc.setError("Desc required"); editTextDesc.requestFocus(); return; } if (sFinishBy.isEmpty()) { editTextFinishBy.setError("Finish by required"); editTextFinishBy.requestFocus(); return; } class SaveTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... voids) { //creating a task Task task = new Task(); task.setTask(sTask); task.setDesc(sDesc); task.setFinishBy(sFinishBy); task.setFinished(false); //adding to database DatabaseClient.getInstance(getApplicationContext()).getAppDatabase() .taskDao() .insert(task); return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); finish(); startActivity(new Intent(getApplicationContext(), MainActivity.class)); Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_LONG).show(); } } SaveTask st = new SaveTask(); st.execute(); } } |
- The above code is very simple, we created an AsyncTask to perform our operation because if we will try to perform the database operation in main thread it will crash our application.
- For saving the task we just created the object and called the insert method that we created in our TaskDao interface.
- You can try testing this but first you need to open this activity and for this you need to code the open functionality in the MainActivity. So what you have to do to test it is, attach a click listener on the add button that we have created in MainActivity and open AddTaskActivity when the add button is clicked.
Reading All Tasks
Now we will read the saved task from the database, and we will display it on the RecyclerView.
RecyclerView Adapter
- Before going further, let’s first create our adapter. So create a class named TasksAdapter and write the following code. If you don’t know about RecyclerView then you can check this Android RecyclerView Tutorial.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package net.simplifiedcoding.mytodo; import android.content.Context; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; public class TasksAdapter extends RecyclerView.Adapter<TasksAdapter.TasksViewHolder> { private Context mCtx; private List<Task> taskList; public TasksAdapter(Context mCtx, List<Task> taskList) { this.mCtx = mCtx; this.taskList = taskList; } @Override public TasksViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_tasks, parent, false); return new TasksViewHolder(view); } @Override public void onBindViewHolder(TasksViewHolder holder, int position) { Task t = taskList.get(position); holder.textViewTask.setText(t.getTask()); holder.textViewDesc.setText(t.getDesc()); holder.textViewFinishBy.setText(t.getFinishBy()); if (t.isFinished()) holder.textViewStatus.setText("Completed"); else holder.textViewStatus.setText("Not Completed"); } @Override public int getItemCount() { return taskList.size(); } class TasksViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView textViewStatus, textViewTask, textViewDesc, textViewFinishBy; public TasksViewHolder(View itemView) { super(itemView); textViewStatus = itemView.findViewById(R.id.textViewStatus); textViewTask = itemView.findViewById(R.id.textViewTask); textViewDesc = itemView.findViewById(R.id.textViewDesc); textViewFinishBy = itemView.findViewById(R.id.textViewFinishBy); itemView.setOnClickListener(this); } @Override public void onClick(View view) { Task task = taskList.get(getAdapterPosition()); Intent intent = new Intent(mCtx, UpdateTaskActivity.class); intent.putExtra("task", task); mCtx.startActivity(intent); } } } |
- In the above code we also attached a click listener to our itemView so that we can fire an event when a task is clicked. When a task is clicked we are opening the UpdateTaskActivity and we are also passing the selected task using intent.
Reading Tasks
- Come inside MainActivity and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package net.simplifiedcoding.mytodo; import android.arch.persistence.room.Room; import android.content.Intent; import android.os.AsyncTask; import android.support.design.widget.FloatingActionButton; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private FloatingActionButton buttonAddTask; private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = findViewById(R.id.recyclerview_tasks); recyclerView.setLayoutManager(new LinearLayoutManager(this)); buttonAddTask = findViewById(R.id.floating_button_add); buttonAddTask.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, AddTaskActivity.class); startActivity(intent); } }); getTasks(); } private void getTasks() { class GetTasks extends AsyncTask<Void, Void, List<Task>> { @Override protected List<Task> doInBackground(Void... voids) { List<Task> taskList = DatabaseClient .getInstance(getApplicationContext()) .getAppDatabase() .taskDao() .getAll(); return taskList; } @Override protected void onPostExecute(List<Task> tasks) { super.onPostExecute(tasks); TasksAdapter adapter = new TasksAdapter(MainActivity.this, tasks); recyclerView.setAdapter(adapter); } } GetTasks gt = new GetTasks(); gt.execute(); } } |
- The code is very simple, we just called the getAll() method that we created inside our TaskDao to get all the stored tasks from the database.
- Then we are displaying the read tasks to a RecyclerView.
Updating and Deleting Task
Now finally the Update and Delete operation we will perform in the UpdateTaskActivity.
- Open UpdateTaskActivity and write the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
package net.simplifiedcoding.mytodo; import android.content.DialogInterface; import android.content.Intent; import android.os.AsyncTask; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class UpdateTaskActivity extends AppCompatActivity { private EditText editTextTask, editTextDesc, editTextFinishBy; private CheckBox checkBoxFinished; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_update_task); editTextTask = findViewById(R.id.editTextTask); editTextDesc = findViewById(R.id.editTextDesc); editTextFinishBy = findViewById(R.id.editTextFinishBy); checkBoxFinished = findViewById(R.id.checkBoxFinished); final Task task = (Task) getIntent().getSerializableExtra("task"); loadTask(task); findViewById(R.id.button_update).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_LONG).show(); updateTask(task); } }); findViewById(R.id.button_delete).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(UpdateTaskActivity.this); builder.setTitle("Are you sure?"); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { deleteTask(task); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); AlertDialog ad = builder.create(); ad.show(); } }); } private void loadTask(Task task) { editTextTask.setText(task.getTask()); editTextDesc.setText(task.getDesc()); editTextFinishBy.setText(task.getFinishBy()); checkBoxFinished.setChecked(task.isFinished()); } private void updateTask(final Task task) { final String sTask = editTextTask.getText().toString().trim(); final String sDesc = editTextDesc.getText().toString().trim(); final String sFinishBy = editTextFinishBy.getText().toString().trim(); if (sTask.isEmpty()) { editTextTask.setError("Task required"); editTextTask.requestFocus(); return; } if (sDesc.isEmpty()) { editTextDesc.setError("Desc required"); editTextDesc.requestFocus(); return; } if (sFinishBy.isEmpty()) { editTextFinishBy.setError("Finish by required"); editTextFinishBy.requestFocus(); return; } class UpdateTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... voids) { task.setTask(sTask); task.setDesc(sDesc); task.setFinishBy(sFinishBy); task.setFinished(checkBoxFinished.isChecked()); DatabaseClient.getInstance(getApplicationContext()).getAppDatabase() .taskDao() .update(task); return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); Toast.makeText(getApplicationContext(), "Updated", Toast.LENGTH_LONG).show(); finish(); startActivity(new Intent(UpdateTaskActivity.this, MainActivity.class)); } } UpdateTask ut = new UpdateTask(); ut.execute(); } private void deleteTask(final Task task) { class DeleteTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... voids) { DatabaseClient.getInstance(getApplicationContext()).getAppDatabase() .taskDao() .delete(task); return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_LONG).show(); finish(); startActivity(new Intent(UpdateTaskActivity.this, MainActivity.class)); } } DeleteTask dt = new DeleteTask(); dt.execute(); } } |
- In the above code, again we are doing the same thing, we are calling the methods we created inside TaskDao using an AsyncTask.
- Now the app is complete you can try running it.
Todo App
- So guys we have build the todo application successfully. From below link you can download the apk file.
Android Room Database Example Source Code
If you are having any problem following this post then you can also get the complete source code from the below link.
So that is all for this Android Room Database Example friends. I will keep posting more tutorial about Android Architecture Components, meanwhile you can share this post with your friends. And for any question leave that on the comment section below. Thank You 🙂