Changes

An Android Room Database and Repository Tutorial

173 bytes added, 18:52, 16 January 2019
no edit summary
<table border="0" cellspacing="0" width="100%"><tr>
<td width="20%">[[The Android Room Persistence Library|Previous]]<td align="center">[[Android Studio Development Essentials - Java Edition|Table of Contents]]<td width="20%" align="right">[[An Accessing Cloud Storage using the Android Room Database and Repository TutorialStorage Access Framework|Next]]</td>
<tr>
<td width="20%">The Android Room Persistence Library<td align="center"><td width="20%" align="right">An Accessing Cloud Storage using the Android Room Database and Repository TutorialStorage Access Framework</td>
</table>
<hr>
This chapter will combine the knowledge gained in the chapter entitled [[The Android Room Persistence Library]] with the initial project created in the previous chapter to provide a detailed tutorial demonstrating how to implement SQLite-based database storage using the Room persistence library. In keeping with the Android architectural guidelines, the project will make use of a view model and repository. The tutorial will make use of all of the elements covered in [[The Android Room Persistence Library]] including entities, a Data Access Object, a Room Databases and asynchronous database queries.
== About About the RoomDemo Project ==
The user interface layout created in the previous chapter was the first step in creating a rudimentary inventory app designed to store the names and quantities of products. When completed, the app will provide the ability to add, delete and search for database entries while also displaying a scrollable list of all products currently stored in the database. This product list will update automatically as database entries are added or deleted.
== Modifying Modifying the Build Configuration ==
Begin by launching Android Studio and opening the RoomDemo project started in the previous chapter. Before adding any new classes to the project, the first step is to add some additional libraries to the build configuration, specifically the Room persistence library and the RecyclerView library. Locate and edit the module level build.gradle file (app -> Gradle Scripts -> build.gradle (Module: app)) and modify it as follows:
</pre>
== Building Building the Entity ==
This project will begin by creating the entity which defines the schema for the database table. The entity will consist of an integer for the product id, a string column to hold the product name and another integer value to store the quantity. The product id column will serve as the primary key and will be auto-generated. Table 68-2 summarizes the structure of the entity:
[[Image:.png]]{| class="wikitable"|-! Column !! Data Type|-| productid || Integer / Primary Key / Auto Increment|-| productname || String|-| productquantity || Integer|}
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
 
public class MainViewModel extends AndroidViewModel
<hr>
<table border="0" cellspacing="0" width="100%"><tr>
<td width="20%">[[The Android Room Persistence Library|Previous]]<td align="center">[[Android Studio Development Essentials - Java Edition|Table of Contents]]<td width="20%" align="right">[[An Accessing Cloud Storage using the Android Room Database and Repository TutorialStorage Access Framework|Next]]</td>
<tr>
<td width="20%">The Android Room Persistence Library<td align="center"><td width="20%" align="right">An Accessing Cloud Storage using the Android Room Database and Repository TutorialStorage Access Framework</td>
</table>