Site icon C1CTech

Android Room Persistence Library Example

<h2> <span style&equals;"font-size&colon; 26&period;5pt&semi; font-family&colon; 'Lucida Sans Unicode'&comma; sans-serif&semi; letter-spacing&colon; -0&period;2pt&semi; color&colon; &num;000080&semi;">Room Persistence Library<&sol;span><&sol;h2>&NewLine;<p>Major problem with <strong><span style&equals;"color&colon; &num;0000ff&semi;">SQLite<&sol;span><&sol;strong> usage is<&sol;p>&NewLine;<ul>&NewLine;<li>There is no <span style&equals;"color&colon; &num;0000ff&semi;"><strong>compile-time<&sol;strong><&sol;span> verification of raw SQL queries&period;For example if you write a SQL query with a wrong <strong><span style&equals;"color&colon; &num;0000ff&semi;">column name<&sol;span><&sol;strong> that does not exist in real database then it will give exception during run time and you can not capture this issue during <strong><span style&equals;"color&colon; &num;0000ff&semi;">compile time<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul>&NewLine;<li>As your schema changes you need to <strong><span style&equals;"color&colon; &num;0000ff&semi;">update<&sol;span><&sol;strong> the affected SQL queries manually&period; This process can be time consuming and error prone&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul>&NewLine;<li>You need to use lots of <strong><span style&equals;"color&colon; &num;0000ff&semi;">boilerplate code<&sol;span><&sol;strong> to convert between SQL queries and Java data objects&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Room takes care of these concerns for you while providing an abstraction layer over SQLite&period;<&sol;span><&sol;strong><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"uvoK3Lap&lowbar;E0" title&equals;"Android Architechture Components Example"><a placeholder href&equals;"http&colon;&sol;&sol;youtu&period;be&sol;uvoK3Lap&lowbar;E0"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;uvoK3Lap&lowbar;E0&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android Architechture Components Example"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p>By creating an event app i will demonstrate you the working of <strong><span style&equals;"color&colon; &num;0000ff&semi;">insert&comma; update&comma; delete<&sol;span><&sol;strong><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;008000&semi;"> and<&sol;span><&sol;span><strong><span style&equals;"color&colon; &num;0000ff&semi;"> retrieve<&sol;span><&sol;strong> functionality using the Room database&period;<em><strong><span style&equals;"color&colon; &num;000080&semi;">Get <&sol;span><&sol;strong><&sol;em><span style&equals;"color&colon; &num;008000&semi;"><strong>GITHUB<&sol;strong><&sol;span><em><strong><span style&equals;"color&colon; &num;000080&semi;"><span style&equals;"color&colon; &num;008000&semi;"> <&sol;span>code from <a href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;EventApp"><span style&equals;"color&colon; &num;008000&semi;">HERE<&sol;span>&period;<&sol;a><&sol;span><&sol;strong><&sol;em><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>There are three major components in Room&colon;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Entity<&sol;strong><&sol;span> &colon; A class annotated with the <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&commat;Entity<&sol;strong><&sol;span><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;arch&sol;persistence&sol;room&sol;Entity&period;html"> <&sol;a> annotation is mapped to a table in database&period; Every entity is persisted in its own table and every field in class represents the column name&period;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>tableName<&sol;strong><&sol;span> attribute is used to define the name of the table<&sol;li>&NewLine;<li>Every entity class must have at-least one <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Primary Key<&sol;strong><&sol;span>  field&comma; annotated with <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&commat;PrimaryKey<&sol;strong><&sol;span><&sol;li>&NewLine;<li>Fields in entity class can be annotated with <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&commat;ColumnInfo<&sol;strong><&sol;span>&lpar;name &equals; &OpenCurlyDoubleQuote;name&lowbar;of&lowbar;column”&rpar; annotation to give specific column names<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong> DAO<&sol;strong><&sol;span> &colon; <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Data Access Object<&sol;strong><&sol;span>  is either be an interface or an abstract class annotated with <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&commat;Doa<&sol;strong><&sol;span> annotation&comma; containing all the methods to define the operations to be performed on data&period; The methods can be annotated with<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;arch&sol;persistence&sol;room&sol;Query&period;html">&commat;Query <&sol;a> <&sol;strong><&sol;span>to retrieve data from database<&sol;li>&NewLine;<li><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;arch&sol;persistence&sol;room&sol;Insert&period;html"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&commat;Insert<&sol;strong><&sol;span> <&sol;a> to insert data into database<&sol;li>&NewLine;<li><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;arch&sol;persistence&sol;room&sol;Delete&period;html"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&commat;Delete<&sol;strong><&sol;span> <&sol;a> to delete data from database<&sol;li>&NewLine;<li><a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;arch&sol;persistence&sol;room&sol;Update&period;html"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>&commat;Update<&sol;strong><&sol;span> <&sol;a> to update data in database<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;000080&semi;">Note<&sol;span>&colon;<&sol;span><&sol;strong>The result of SQLite queries are composed into <span style&equals;"color&colon; &num;0000ff&semi;"><strong>cursor<&sol;strong><&sol;span> object&comma; DAO methods abstract the conversion of cursor to <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Entity objects and vice-versa&period;<&sol;strong><&sol;span><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span style&equals;"color&colon; &num;000080&semi;">Database<&sol;span> &colon;<&sol;strong><&sol;span> Database is a container for tables&period; An abstract class annotated with <a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;arch&sol;persistence&sol;room&sol;Database&period;html"><strong>&commat;Database<&sol;strong> <&sol;a> annotation is used to create a database with given name along with database version&period;<&sol;p>&NewLine;<ul>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>version<&sol;strong><&sol;span> &equals; <span style&equals;"color&colon; &num;0000ff&semi;">intValueForDBVersion<&sol;span> is used to define the database version<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">entities<&sol;span> <&sol;strong>&equals;<span style&equals;"color&colon; &num;0000ff&semi;"> &lbrace;EntityClassOne&period;class&comma; &&num;8230&semi;&period;&rcub;<&sol;span> is used to define list of entities for database<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2><span style&equals;"color&colon; &num;000080&semi;">Building an Event App<&sol;span><&sol;h2>&NewLine;<p>We will build a Event App that will allow the user to&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Create<&sol;span><&sol;strong> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Save<&sol;strong><&sol;span> event in database<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Display<&sol;strong><&sol;span> a list of events<&sol;li>&NewLine;<li><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Update<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Delete<&sol;strong><&sol;span> event<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2><span style&equals;"color&colon; &num;000080&semi;">Creating New Project<&sol;span><&sol;h2>&NewLine;<p>1&period;In Android Studio&comma; go to <span style&equals;"color&colon; &num;0000ff&semi;"><strong>File <&sol;strong><strong>&Implies;<&sol;strong><strong> New Project<&sol;strong><&sol;span> and fill all the details required to create a new project&period; When it prompts to select a default activity&comma; select <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Blank Activity<&sol;strong><&sol;span> and proceed&period;<&sol;p>&NewLine;<p>2&period;Open <span style&equals;"color&colon; &num;0000ff&semi;"><strong>build&period;gradle<&sol;strong><&sol;span> <span style&equals;"color&colon; &num;0000ff&semi;">&lpar;Module&colon;app&rpar; <span style&equals;"color&colon; &num;000000&semi;"> and add room library dependency&period;<&sol;span><&sol;span><&sol;p>&NewLine;<pre>dependencies &lbrace;&NewLine; &NewLine; implementation 'com&period;android&period;support&colon;support-v4&colon;26&period;1&period;0'&NewLine; compile 'com&period;android&period;support&colon;design&colon;26&period;1&period;0'&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;room dependency<&sol;span><&sol;strong>&NewLine; compile 'android&period;arch&period;persistence&period;room&colon;runtime&colon;1&period;0&period;0'&NewLine; annotationProcessor 'android&period;arch&period;persistence&period;room&colon;compiler&colon;1&period;0&period;0'&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;recyclerview dependency<&sol;span><&sol;strong>&NewLine; compile 'com&period;android&period;support&colon;recyclerview-v7&colon;26&period;1&period;0'&NewLine; &NewLine;&rcub;<&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;">Create Entity<&sol;span><&sol;h3>&NewLine;<p>3&period;  Before creating a database&comma; Let&&num;8217&semi;s create an <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Entity<&sol;strong><&sol;span>&comma; named as <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Event<&sol;strong><&sol;span> and later&comma; Objects of this class will be added to database&period;<&sol;p>&NewLine;<p>To do this&colon;<&sol;p>&NewLine;<ul>&NewLine;<li>Create a class named <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Event<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li>Add <strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;Entity<&sol;span><&sol;strong> annotation on the class&period;<&sol;li>&NewLine;<li>Add<span style&equals;"color&colon; &num;0000ff&semi;"><strong> ID&comma; title&comma;description and date fields<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li><strong><span style&equals;"color&colon; &num;0000ff&semi;">Important&colon;<&sol;span><&sol;strong> mark at least one field with <span style&equals;"color&colon; &num;0000ff&semi;"><strong>&commat;PrimaryKey<&sol;strong><&sol;span> annotation&period;<&sol;li>&NewLine;<li>Use alt&plus;insert to implement constructor&comma; override <strong><span style&equals;"color&colon; &num;0000ff&semi;">getter and setter<&sol;span><&sol;strong>&comma; and optionally override equals or <strong><span style&equals;"color&colon; &num;0000ff&semi;">toString<&sol;span><&sol;strong>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;000080&semi;">Event&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;eventapp&period;entity&semi;&NewLine;&NewLine;import android&period;arch&period;persistence&period;room&period;Entity&semi;&NewLine;import android&period;arch&period;persistence&period;room&period;Ignore&semi;&NewLine;import android&period;arch&period;persistence&period;room&period;PrimaryKey&semi;&NewLine;&NewLine;import static com&period;example&period;lenovo&period;eventapp&period;entity&period;Event&period;TABLE&lowbar;NAME&semi;&NewLine;&NewLine;&NewLine;&commat;Entity&lpar;tableName &equals; TABLE&lowbar;NAME&rpar;&NewLine;public class Event &lbrace;&NewLine;&NewLine;&NewLine; public static final String TABLE&lowbar;NAME &equals; "events"&semi;&NewLine;&NewLine;&NewLine; &commat;PrimaryKey&lpar;autoGenerate &equals; true&rpar;&NewLine; private int id&semi;&NewLine; private String title&semi;&NewLine; private String description&semi;&NewLine; private String date&semi;&NewLine;&NewLine; public Event&lpar;&rpar; &lbrace;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Ignore&NewLine; public Event&lpar;int id&comma; String title&comma; String description&rpar; &lbrace;&NewLine; this&period;id &equals; id&semi;&NewLine; this&period;title &equals; title&semi;&NewLine; this&period;description &equals; description&semi;&NewLine; this&period;date &equals; date&semi;&NewLine; &rcub;&NewLine;&NewLine; public int getId&lpar;&rpar; &lbrace;&NewLine; return id&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setId&lpar;int id&rpar; &lbrace;&NewLine; this&period;id &equals; id&semi;&NewLine; &rcub;&NewLine;&NewLine; public String getDate&lpar;&rpar; &lbrace;&NewLine; return date&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setDate&lpar;String date&rpar; &lbrace;&NewLine; this&period;date &equals; date&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setTitle&lpar;String title&rpar; &lbrace;&NewLine; this&period;title &equals; title&semi;&NewLine; &rcub;&NewLine;&NewLine; public void setDescription&lpar;String description&rpar; &lbrace;&NewLine; this&period;description &equals; description&semi;&NewLine; &rcub;&NewLine;&NewLine; public String getDescription&lpar;&rpar; &lbrace;&NewLine; return description&semi;&NewLine; &rcub;&NewLine;&NewLine; public String getTitle&lpar;&rpar; &lbrace;&NewLine; return title&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public String toString&lpar;&rpar; &lbrace;&NewLine; return "Event&lbrace;" &plus;&NewLine; "id&equals;" &plus; id &plus;&NewLine; "&comma; name&equals;'" &plus; title &plus; '&bsol;'' &plus;&NewLine; "&comma; description&equals;'" &plus; description &plus; '&bsol;'' &plus;&NewLine; "&comma; date&equals;'" &plus; date &plus; '&bsol;'' &plus;&NewLine; '&rcub;'&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;">Creating DAO<&sol;span><&sol;h3>&NewLine;<p data-id&equals;"2b4490ec4010bc79289802cd29ae51961"><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;000000&semi;">4&period;<&sol;span><strong> DAOs<&sol;strong><&sol;span> define all methods to access database&comma; annotated with<strong><span style&equals;"color&colon; &num;0000ff&semi;"> &commat;Dao <&sol;span><&sol;strong>annotation&period; The DAO perform <span style&equals;"color&colon; &num;0000ff&semi;"><strong>CRUD<&sol;strong><&sol;span> operations on data within a database&period;<&sol;p>&NewLine;<p data-id&equals;"cb31f2e83efc2f765040e254529e5a581">The following code will&colon;<&sol;p>&NewLine;<ul data-id&equals;"016587df1af9f6c84aefd44f0131f4f91">&NewLine;<li>Create an interface&comma; marked with <strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;Dao <&sol;span><&sol;strong>annotation&period;<&sol;li>&NewLine;<li>Add methods for <span style&equals;"color&colon; &num;0000ff&semi;"><strong>CRUD<&sol;strong><&sol;span> operations&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><strong><span style&equals;"color&colon; &num;000080&semi;">EventDao&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;eventapp&period;dao&semi;&NewLine;&NewLine;import android&period;arch&period;persistence&period;room&period;Dao&semi;&NewLine;import android&period;arch&period;persistence&period;room&period;Delete&semi;&NewLine;import android&period;arch&period;persistence&period;room&period;Insert&semi;&NewLine;import android&period;arch&period;persistence&period;room&period;Query&semi;&NewLine;import android&period;arch&period;persistence&period;room&period;Update&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;eventapp&period;entity&period;Event&semi;&NewLine;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;&NewLine;&commat;Dao&NewLine;public interface EventDao &lbrace;&NewLine;&NewLine; &commat;Query&lpar;"SELECT &ast; FROM " &plus; Event&period;TABLE&lowbar;NAME &rpar;&NewLine; List&lt&semi;Event&gt&semi; getEvents&lpar;&rpar;&semi;&NewLine;&NewLine; &commat;Insert&NewLine; void addEvent&lpar;Event event&rpar;&semi;&NewLine;&NewLine; &commat;Delete&NewLine; void deleteEvent&lpar;Event event&rpar;&semi;&NewLine;&NewLine; &commat;Update&NewLine; void updateEvent&lpar;Event event&rpar;&semi;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<h3><span style&equals;"color&colon; &num;000080&semi;">Create Database<&sol;span><&sol;h3>&NewLine;<p class&equals;"graf" style&equals;"background&colon; white&semi; margin&colon; 18&period;15pt 0cm &period;0001pt 0cm&semi;">5&period; Now&comma; we have table defined as Entity and CRUD methods defined via <strong><span style&equals;"color&colon; &num;0000ff&semi;">EventDao<&sol;span><&sol;strong>&period;<&sol;p>&NewLine;<p>We will have to&colon;<&sol;p>&NewLine;<ul data-id&equals;"d83ab6bf78f9af9140f0f2475ca1a7911">&NewLine;<li>Create an abstract class <strong><span style&equals;"color&colon; &num;0000ff&semi;">EventDatabse<&sol;span><&sol;strong> which extends <span style&equals;"color&colon; &num;0000ff&semi;"><strong>RoomDatabase<&sol;strong><&sol;span>&period;<&sol;li>&NewLine;<li>Add version and entities to database as <strong><span style&equals;"color&colon; &num;0000ff&semi;">&commat;Database<&sol;span><&sol;strong>&lpar;entities &equals; &lbrace;Event&period;class&rcub;&comma; version &equals; 1&rpar;&period;<&sol;li>&NewLine;<li>Add abstract methods of all DAO&&num;8217&semi;s where the returned DAO object will be constructed by Room for database interactions&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p data-id&equals;"e23af27d8024cfc3eb5d65ed425d3bd41">Some things to remember&colon;<&sol;p>&NewLine;<ul data-id&equals;"e5ad1fa4025df4bb7568fdd72492658a1">&NewLine;<li>Version number is changed to update the database structure&comma; when required in future updates<&sol;li>&NewLine;<li>The database file name must end with the &period;db extension<&sol;li>&NewLine;<li>Creating instance of database is quite costly so we will apply a <a href&equals;"https&colon;&sol;&sol;en&period;wikipedia&period;org&sol;wiki&sol;Singleton&lowbar;pattern"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Singleton Pattern<&sol;span><&sol;strong> <&sol;a> to create and use already instantiated single instance for every database access&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>EventDatabase&period;Java<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;eventapp&period;db&semi;&NewLine;&NewLine;import android&period;arch&period;persistence&period;room&period;Database&semi;&NewLine;import android&period;arch&period;persistence&period;room&period;Room&semi;&NewLine;import android&period;arch&period;persistence&period;room&period;RoomDatabase&semi;&NewLine;import android&period;content&period;Context&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;eventapp&period;dao&period;EventDao&semi;&NewLine;import com&period;example&period;lenovo&period;eventapp&period;entity&period;Event&semi;&NewLine;&NewLine;&NewLine;&commat;Database&lpar;entities &equals; &lbrace;Event&period;class&rcub;&comma;version &equals; 1&rpar;&NewLine;public abstract class EventDatabase extends RoomDatabase&lbrace;&NewLine; private static final String DB&lowbar;NAME &equals; "Event&lowbar;Database&period;db"&semi;&NewLine; private static EventDatabase INSTANCE&semi;&NewLine; public abstract EventDao eventDao&lpar;&rpar;&semi;&NewLine;&NewLine; public static EventDatabase getEventDatabase&lpar;Context context&rpar; &lbrace;&NewLine; if &lpar;INSTANCE &equals;&equals; null&rpar; &lbrace;&NewLine; INSTANCE &equals; Room&period;databaseBuilder&lpar;context&comma; EventDatabase&period;class&comma; DB&lowbar;NAME&rpar;&period;build&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; return INSTANCE&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Note&colon;<&sol;strong><&sol;span> It is good to note that Room does not allow code execution on Main thread&period; Instead&comma; <strong><span style&equals;"color&colon; &num;0000ff&semi;">allowMainThreadQueries<&sol;span><&sol;strong> is used to allow the execution&period; However&comma; using this is not recommended on real apps&period; You must perform queries on  <strong><span style&equals;"color&colon; &num;0000ff&semi;">AsyncTask &lpar;or handler&comma; rxjava&rpar;&comma;<&sol;span><&sol;strong> otherwise your application will<strong><span style&equals;"color&colon; &num;0000ff&semi;"> crash<&sol;span> <&sol;strong>or cause <strong><span style&equals;"color&colon; &num;0000ff&semi;">ANR<&sol;span><&sol;strong> problems&period;<&sol;p>&NewLine;<h2><span style&equals;"color&colon; &num;000080&semi;">Implement Database Interactions<&sol;span><&sol;h2>&NewLine;<p>6&period;  The below snippet will demonstrate the working of <strong><span style&equals;"color&colon; &num;0000ff&semi;">insert&comma; update&comma; and delete<&sol;span><&sol;strong> functionality using the Room database&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Add Event<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>private static final String DB&lowbar;NAME &equals; "Event&lowbar;Database&period;db"&semi;&NewLine;private static EventDatabase INSTANCE&semi;&NewLine;public abstract class EventDatabase extends RoomDatabase&lbrace;&NewLine; private static final String DB&lowbar;NAME &equals; "Event&lowbar;Database&period;db"&semi;&NewLine; private static EventDatabase INSTANCE&semi;&NewLine; public abstract EventDao eventDao&lpar;&rpar;&semi;&NewLine;&NewLine; public static EventDatabase getEventDatabase&lpar;Context context&rpar; &lbrace;&NewLine; if &lpar;INSTANCE &equals;&equals; null&rpar; &lbrace;&NewLine; INSTANCE &equals; Room&period;databaseBuilder&lpar;context&comma; EventDatabase&period;class&comma; DB&lowbar;NAME&rpar;&period;build&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; return INSTANCE&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine;&rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;add event into the database<&sol;span><&sol;strong>&NewLine;EventDatabase&period;getEventDatabase&lpar;getApplicationContext&lpar;&rpar;&rpar;&period;eventDao&lpar;&rpar;&period;addEvent&lpar;event&rpar;&semi;&NewLine;&NewLine;<&sol;pre>&NewLine;<p><img class&equals;"aligncenter wp-image-572 size-large" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;05&sol;Screenshot&lowbar;1530085350-576x1024&period;png" alt&equals;"" width&equals;"576" height&equals;"1024" &sol;><br &sol;>&NewLine;<strong><span style&equals;"color&colon; &num;000080&semi;">Retrieve And Display EventList<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>private static final String DB&lowbar;NAME &equals; "Event&lowbar;Database&period;db"&semi;&NewLine;private static EventDatabase INSTANCE&semi;&NewLine;&NewLine;public abstract class EventDatabase extends RoomDatabase&lbrace;&NewLine; private static final String DB&lowbar;NAME &equals; "Event&lowbar;Database&period;db"&semi;&NewLine; private static EventDatabase INSTANCE&semi;&NewLine; public abstract EventDao eventDao&lpar;&rpar;&semi;&NewLine;&NewLine; public static EventDatabase getEventDatabase&lpar;Context context&rpar; &lbrace;&NewLine; if &lpar;INSTANCE &equals;&equals; null&rpar; &lbrace;&NewLine; INSTANCE &equals; Room&period;databaseBuilder&lpar;context&comma; EventDatabase&period;class&comma; DB&lowbar;NAME&rpar;&period;build&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; return INSTANCE&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine;&rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;get events from database<&sol;span><&sol;strong>&NewLine;List&lt&semi;Event&gt&semi; events &equals; EventDatabase&period;getEventDatabase&lpar;getApplicationContext&lpar;&rpar;&rpar;&period;eventDao&lpar;&rpar;&period;getEvents&lpar;&rpar;&semi;&NewLine;adapter &equals; new EventsAdapter&lpar;events&comma; getApplicationContext&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine;recyclerView&period;setAdapter&lpar;adapter&rpar;&semi;&NewLine;&NewLine;<&sol;pre>&NewLine;<p><img class&equals;"alignnone size-medium wp-image-571" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;05&sol;Screenshot&lowbar;1530085104-169x300&period;png" alt&equals;"" width&equals;"169" height&equals;"300" &sol;>     <img class&equals;"alignnone size-medium wp-image-575" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;05&sol;Screenshot&lowbar;1530085640-169x300&period;png" alt&equals;"" width&equals;"169" height&equals;"300" &sol;><&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong>Update Event<&sol;strong><&sol;span><&sol;p>&NewLine;<h3><&sol;h3>&NewLine;<pre>private static final String DB&lowbar;NAME &equals; "Event&lowbar;Database&period;db"&semi;&NewLine;private static EventDatabase INSTANCE&semi;&NewLine;&NewLine;public abstract class EventDatabase extends RoomDatabase&lbrace;&NewLine; private static final String DB&lowbar;NAME &equals; "Event&lowbar;Database&period;db"&semi;&NewLine; private static EventDatabase INSTANCE&semi;&NewLine; public abstract EventDao eventDao&lpar;&rpar;&semi;&NewLine;&NewLine; public static EventDatabase getEventDatabase&lpar;Context context&rpar; &lbrace;&NewLine; if &lpar;INSTANCE &equals;&equals; null&rpar; &lbrace;&NewLine; INSTANCE &equals; Room&period;databaseBuilder&lpar;context&comma; EventDatabase&period;class&comma; DB&lowbar;NAME&rpar;&period;build&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; return INSTANCE&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine;&rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;update event into the database<&sol;span><&sol;strong>&NewLine;EventDatabase&period;getEventDatabase&lpar;getApplicationContext&lpar;&rpar;&rpar;&period;eventDao&lpar;&rpar;&period;updateEvent&lpar;event&rpar;&semi;<&sol;pre>&NewLine;<p><img class&equals;"alignnone size-medium wp-image-573" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;05&sol;Screenshot&lowbar;1530085371-169x300&period;png" alt&equals;"" width&equals;"169" height&equals;"300" &sol;>      <img class&equals;"alignnone size-medium wp-image-574" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2018&sol;05&sol;Screenshot&lowbar;1530085392-169x300&period;png" alt&equals;"" width&equals;"169" height&equals;"300" &sol;><&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;000080&semi;">Delete Event <&sol;span><&sol;strong><&sol;p>&NewLine;<pre>private static final String DB&lowbar;NAME &equals; "Event&lowbar;Database&period;db"&semi;&NewLine;private static EventDatabase INSTANCE&semi;&NewLine;public abstract class EventDatabase extends RoomDatabase&lbrace;&NewLine; private static final String DB&lowbar;NAME &equals; "Event&lowbar;Database&period;db"&semi;&NewLine; private static EventDatabase INSTANCE&semi;&NewLine; public abstract EventDao eventDao&lpar;&rpar;&semi;&NewLine;&NewLine; public static EventDatabase getEventDatabase&lpar;Context context&rpar; &lbrace;&NewLine; if &lpar;INSTANCE &equals;&equals; null&rpar; &lbrace;&NewLine; INSTANCE &equals; Room&period;databaseBuilder&lpar;context&comma; EventDatabase&period;class&comma; DB&lowbar;NAME&rpar;&period;build&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; return INSTANCE&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine;&rcub;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;delete event from database<&sol;span><&sol;strong>&NewLine;EventDatabase&period;getEventDatabase&lpar;getApplicationContext&lpar;&rpar;&rpar;&period;eventDao&lpar;&rpar;&period;deleteEvent&lpar;event&rpar;&semi;<&sol;pre>&NewLine;<p>7&period;  Open <span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span> and write the below code &period;It consist of<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">Recyclerview<&sol;span><&sol;strong> &colon; Shows list of events&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Textview<&sol;strong><&sol;span> &colon; Which shows <span style&equals;"color&colon; &num;0000ff&semi;"><strong>no event found <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">text<&sol;span><&sol;span> when list contains no item&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">FloatingButton<&sol;span><&sol;strong> &colon; When we click on it &comma;It opens alertdialog  to add event into the database&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;android&period;support&period;constraint&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;v7&period;widget&period;RecyclerView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;recycler&lowbar;view&lowbar;list&lowbar;events"&NewLine; android&colon;layout&lowbar;width&equals;"0dp"&NewLine; android&colon;layout&lowbar;height&equals;"0dp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintLeft&lowbar;toLeftOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;android&period;support&period;v7&period;widget&period;RecyclerView&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;no&lowbar;events&lowbar;found"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;fontFamily&equals;"sans-serif"&NewLine; android&colon;gravity&equals;"center"&NewLine; android&colon;text&equals;"NO EVENTS FOUND&excl;"&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; android&colon;textSize&equals;"25sp"&NewLine; android&colon;visibility&equals;"gone" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;android&period;support&period;design&period;widget&period;FloatingActionButton&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;fab&lowbar;add"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"16dp"&NewLine; android&colon;layout&lowbar;marginRight&equals;"16dp"&NewLine; app&colon;elevation&equals;"4dp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintRight&lowbar;toRightOf&equals;"parent"&NewLine; app&colon;srcCompat&equals;"&commat;drawable&sol;ic&lowbar;add&lowbar;black&lowbar;24dp" &sol;&gt&semi;&NewLine;&lt&semi;&sol;android&period;support&period;constraint&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;<p>8&period;  Open <strong><span style&equals;"color&colon; &num;0000ff&semi;">event&lowbar;dialog&period;xml <&sol;span><&sol;strong><span style&equals;"color&colon; &num;000000&semi;">and write the below code&period;On update and add event I have set <strong><span style&equals;"color&colon; &num;0000ff&semi;">event&lowbar;dialog&period;xml <&sol;span><&sol;strong><&sol;span>view to alertdialog&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">event&lowbar;dialog&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;LinearLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; android&colon;orientation&equals;"vertical"&NewLine; android&colon;padding&equals;"8dp"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;dialog&lowbar;title"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"10dp"&NewLine; android&colon;fontFamily&equals;"sans-serif-medium"&NewLine; android&colon;text&equals;"New Event"&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorAccent"&NewLine; android&colon;textSize&equals;"22sp"&NewLine; android&colon;textStyle&equals;"normal" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;EditText&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;edt&lowbar;title"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&commat;android&colon;color&sol;transparent"&NewLine; android&colon;fontFamily&equals;"sans-serif"&NewLine; android&colon;gravity&equals;"top"&NewLine; android&colon;hint&equals;"Title"&NewLine; android&colon;inputType&equals;"textCapSentences&vert;textMultiLine"&NewLine; android&colon;lines&equals;"2"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;black"&NewLine; android&colon;textColorHint&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; android&colon;textSize&equals;"18sp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;EditText&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;edt&lowbar;discription"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;background&equals;"&commat;android&colon;color&sol;transparent"&NewLine; android&colon;fontFamily&equals;"sans-serif"&NewLine; android&colon;gravity&equals;"top"&NewLine; android&colon;hint&equals;"Discription"&NewLine; android&colon;inputType&equals;"textCapSentences&vert;textMultiLine"&NewLine; android&colon;lines&equals;"3"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;black"&NewLine; android&colon;textColorHint&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; android&colon;textSize&equals;"18sp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;RelativeLayout&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;date"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentLeft&equals;"true"&NewLine; android&colon;layout&lowbar;marginLeft&equals;"8dp"&NewLine; android&colon;fontFamily&equals;"sans-serif"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;black"&NewLine; android&colon;textSize&equals;"18sp" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;setdate"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentRight&equals;"true"&NewLine; android&colon;background&equals;"&commat;color&sol;colorAccent"&NewLine; android&colon;text&equals;"set"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;&sol;RelativeLayout&gt&semi;&NewLine;&lt&semi;&sol;LinearLayout&gt&semi;<&sol;pre>&NewLine;<p>9&period;  <span style&equals;"color&colon; &num;0000ff&semi;"><strong>list&lowbar;item&lowbar;event&period;xml <&sol;strong><span style&equals;"color&colon; &num;000000&semi;">represents the single list item of recyclerview&period;<br &sol;>&NewLine;<&sol;span><&sol;span><br &sol;>&NewLine;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>list&lowbar;item&lowbar;event&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;RelativeLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"8dp"&NewLine; android&colon;padding&equals;"8dp"&gt&semi;&NewLine;&NewLine; &lt&semi;LinearLayout&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentLeft&equals;"true"&NewLine; android&colon;orientation&equals;"vertical"&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;title"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;fontFamily&equals;"sans-serif"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;orange&lowbar;dark"&NewLine; android&colon;textSize&equals;"20dp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;discription"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"6dp"&NewLine; android&colon;fontFamily&equals;"sans-serif"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;holo&lowbar;green&lowbar;light"&NewLine; android&colon;textSize&equals;"20dp" &sol;&gt&semi;&NewLine; &lt&semi;&sol;LinearLayout&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;timestamp"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;alignParentRight&equals;"true"&NewLine; android&colon;fontFamily&equals;"sans-serif"&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorAccent"&NewLine; android&colon;textSize&equals;"20dp"&NewLine; android&colon;textStyle&equals;"bold" &sol;&gt&semi;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;RelativeLayout&gt&semi;<&sol;pre>&NewLine;<p>10&period;  Creating adapter class for recyclerview&comma; open <strong><span style&equals;"color&colon; &num;0000ff&semi;">EventsAdapter&period;Java <&sol;span><&sol;strong><span style&equals;"color&colon; &num;000000&semi;">and write the below code&period;<br &sol;>&NewLine;<&sol;span><br &sol;>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">EventsAdapter&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;eventapp&semi;&NewLine;&NewLine;import android&period;content&period;Context&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;view&period;ViewGroup&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;&NewLine;import com&period;example&period;lenovo&period;eventapp&period;entity&period;Event&semi;&NewLine;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;&NewLine;public class EventsAdapter extends RecyclerView&period;Adapter&lt&semi;EventsAdapter&period;EventViewHolder&gt&semi; &lbrace;&NewLine; private final Context context&semi;&NewLine; private List&lt&semi;Event&gt&semi; items&semi;&NewLine;&NewLine;&NewLine; public EventsAdapter&lpar;List&lt&semi;Event&gt&semi; items&comma; Context context&rpar; &lbrace;&NewLine; this&period;items &equals; items&semi;&NewLine; this&period;context &equals; context&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; public class EventViewHolder extends RecyclerView&period;ViewHolder &lbrace;&NewLine; TextView titleTextView&semi;&NewLine; TextView dateTextView&semi;&NewLine; TextView descriptionTextView&semi;&NewLine;&NewLine; EventViewHolder&lpar;View v&rpar; &lbrace;&NewLine; super&lpar;v&rpar;&semi;&NewLine; titleTextView &equals; &lpar;TextView&rpar; v&period;findViewById&lpar;R&period;id&period;tv&lowbar;title&rpar;&semi;&NewLine; dateTextView &equals; &lpar;TextView&rpar; v&period;findViewById&lpar;R&period;id&period;tv&lowbar;timestamp&rpar;&semi;&NewLine; descriptionTextView &equals; &lpar;TextView&rpar; v&period;findViewById&lpar;R&period;id&period;tv&lowbar;discription&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public EventViewHolder onCreateViewHolder&lpar;ViewGroup parent&comma;&NewLine; int viewType&rpar; &lbrace;&NewLine; View v &equals; LayoutInflater&period;from&lpar;context&rpar;&NewLine; &period;inflate&lpar;R&period;layout&period;list&lowbar;item&lowbar;event&comma; parent&comma; false&rpar;&semi;&NewLine;&NewLine; return new EventViewHolder&lpar;v&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onBindViewHolder&lpar;EventViewHolder holder&comma; int position&rpar; &lbrace;&NewLine; Event item &equals; items&period;get&lpar;position&rpar;&semi;&NewLine; holder&period;titleTextView&period;setText&lpar;item&period;getTitle&lpar;&rpar;&rpar;&semi;&NewLine; holder&period;descriptionTextView&period;setText&lpar;item&period;getDescription&lpar;&rpar;&rpar;&semi;&NewLine; holder&period;dateTextView&period;setText&lpar;item&period;getDate&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine; &commat;Override&NewLine; public int getItemCount&lpar;&rpar; &lbrace;&NewLine;&NewLine; return items&period;size&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<p>11&period;  Open <strong><span style&equals;"color&colon; &num;0000ff&semi;">RecyclerTouchListener&period;Java <&sol;span><&sol;strong><span style&equals;"color&colon; &num;000000&semi;">and write the below code in which <&sol;span>I have created custom onClickListener for recyclerview single item <span style&equals;"color&colon; &num;008000&semi;">click&period;<&sol;span><br &sol;>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;"><br &sol;>&NewLine;RecyclerTouchListener&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;eventapp&semi;&NewLine;&NewLine;import android&period;content&period;Context&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;view&period;GestureDetector&semi;&NewLine;import android&period;view&period;MotionEvent&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;&NewLine;&NewLine;public class RecyclerTouchListener implements RecyclerView&period;OnItemTouchListener &lbrace;&NewLine;&NewLine; private ClickListener clicklistener&semi;&NewLine; private GestureDetector gestureDetector&semi;&NewLine;&NewLine; public RecyclerTouchListener&lpar;Context context&comma; final RecyclerView recycleView&comma; final ClickListener clicklistener&rpar; &lbrace;&NewLine;&NewLine; this&period;clicklistener &equals; clicklistener&semi;&NewLine; gestureDetector &equals; new GestureDetector&lpar;context&comma; new GestureDetector&period;SimpleOnGestureListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public boolean onSingleTapUp&lpar;MotionEvent e&rpar; &lbrace;&NewLine; return true&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onLongPress&lpar;MotionEvent e&rpar; &lbrace;&NewLine; View child &equals; recycleView&period;findChildViewUnder&lpar;e&period;getX&lpar;&rpar;&comma; e&period;getY&lpar;&rpar;&rpar;&semi;&NewLine; if &lpar;child &excl;&equals; null &amp&semi;&amp&semi; clicklistener &excl;&equals; null&rpar; &lbrace;&NewLine; clicklistener&period;onLongClick&lpar;child&comma; recycleView&period;getChildAdapterPosition&lpar;child&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public boolean onInterceptTouchEvent&lpar;RecyclerView rv&comma; MotionEvent e&rpar; &lbrace;&NewLine; View child &equals; rv&period;findChildViewUnder&lpar;e&period;getX&lpar;&rpar;&comma; e&period;getY&lpar;&rpar;&rpar;&semi;&NewLine; if &lpar;child &excl;&equals; null &amp&semi;&amp&semi; clicklistener &excl;&equals; null &amp&semi;&amp&semi; gestureDetector&period;onTouchEvent&lpar;e&rpar;&rpar; &lbrace;&NewLine; clicklistener&period;onClick&lpar;child&comma; rv&period;getChildAdapterPosition&lpar;child&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; return false&semi;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onTouchEvent&lpar;RecyclerView rv&comma; MotionEvent e&rpar; &lbrace;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onRequestDisallowInterceptTouchEvent&lpar;boolean disallowIntercept&rpar; &lbrace;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; public interface ClickListener &lbrace;&NewLine; void onClick&lpar;View view&comma; int position&rpar;&semi;&NewLine;&NewLine; void onLongClick&lpar;View view&comma; int position&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>12&period;  Open <strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;Java <&sol;span><&sol;strong><span style&equals;"color&colon; &num;0000ff&semi;"><span style&equals;"color&colon; &num;000000&semi;">and write the below code&period;The below snippet will demonstrate the working of <span style&equals;"color&colon; &num;0000ff&semi;"><strong>insert&comma; update&comma; delete<&sol;strong><&sol;span> and <span style&equals;"color&colon; &num;0000ff&semi;"><strong>retrieve list of events<&sol;strong><&sol;span> functionality using the Room database in DatabaseAsync worker thread&period;<br &sol;>&NewLine;<&sol;span><&sol;span><br &sol;>&NewLine;<strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;Java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;example&period;lenovo&period;eventapp&semi;&NewLine;&NewLine;import android&period;app&period;AlertDialog&semi;&NewLine;import android&period;app&period;DatePickerDialog&semi;&NewLine;import android&period;app&period;Dialog&semi;&NewLine;import android&period;content&period;DialogInterface&semi;&NewLine;import android&period;os&period;AsyncTask&semi;&NewLine;import android&period;support&period;design&period;widget&period;FloatingActionButton&semi;&NewLine;import android&period;support&period;v4&period;app&period;DialogFragment&semi;&NewLine;import android&period;support&period;v7&period;app&period;AppCompatActivity&semi;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;support&period;v7&period;widget&period;DefaultItemAnimator&semi;&NewLine;import android&period;support&period;v7&period;widget&period;LinearLayoutManager&semi;&NewLine;import android&period;support&period;v7&period;widget&period;RecyclerView&semi;&NewLine;import android&period;text&period;TextUtils&semi;&NewLine;import android&period;view&period;LayoutInflater&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;Button&semi;&NewLine;import android&period;widget&period;DatePicker&semi;&NewLine;import android&period;widget&period;EditText&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;import android&period;widget&period;Toast&semi;&NewLine;import com&period;example&period;lenovo&period;eventapp&period;db&period;EventDatabase&semi;&NewLine;import com&period;example&period;lenovo&period;eventapp&period;entity&period;Event&semi;&NewLine;import java&period;text&period;SimpleDateFormat&semi;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;import java&period;util&period;Calendar&semi;&NewLine;import java&period;util&period;Date&semi;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; RecyclerView recyclerView&semi;&NewLine; TextView noEventsFound&semi;&NewLine; FloatingActionButton fab&semi;&NewLine; EventsAdapter adapter&semi;&NewLine; List&lt&semi;Event&gt&semi; events &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine; private static TextView tv&lowbar;date&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&semi;&NewLine;&NewLine; noEventsFound &equals; &lpar;TextView&rpar; findViewById&lpar;R&period;id&period;tv&lowbar;no&lowbar;events&lowbar;found&rpar;&semi;&NewLine;&NewLine; recyclerView &equals; &lpar;RecyclerView&rpar; findViewById&lpar;R&period;id&period;recycler&lowbar;view&lowbar;list&lowbar;events&rpar;&semi;&NewLine;&NewLine; recyclerView&period;setItemAnimator&lpar;new DefaultItemAnimator&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; recyclerView&period;setLayoutManager&lpar;new LinearLayoutManager&lpar;getApplicationContext&lpar;&rpar;&comma; LinearLayoutManager&period;VERTICAL&comma; false&rpar;&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;show list of events if exists in database<&sol;strong><&sol;span>&NewLine; new DatabaseAsync&lpar;&rpar;&period;execute&lpar;null&comma; -1&comma; null&comma; null&comma; null&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;shows NO EVENTS FOUND when list is empty<&sol;span><&sol;strong>&NewLine; checkListEmptyOrNot&lpar;&rpar;&semi;&NewLine;&NewLine; recyclerView&period;addOnItemTouchListener&lpar;new RecyclerTouchListener&lpar;getApplicationContext&lpar;&rpar;&comma; recyclerView&comma; new RecyclerTouchListener&period;ClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View view&comma; int position&rpar; &lbrace;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; public void onLongClick&lpar;View view&comma; int position&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;show Alertdialog to edit or update the event<&sol;span><&sol;strong>&NewLine; showActionsDialog&lpar;position&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;add listener on FloatingActionButton to add event<&sol;strong><&sol;span>&NewLine; fab &equals; &lpar;FloatingActionButton&rpar; findViewById&lpar;R&period;id&period;fab&lowbar;add&rpar;&semi;&NewLine;&NewLine; fab&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View view&rpar; &lbrace;&NewLine; &sol;&sol;adding new event&NewLine; showEventDialog&lpar;false&comma; null&comma; -1&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private class DatabaseAsync extends AsyncTask&lt&semi;Object&comma; Void&comma; List&lt&semi;Event&gt&semi;&gt&semi; &lbrace;&NewLine;&NewLine; &commat;Override&NewLine; protected void onPreExecute&lpar;&rpar; &lbrace;&NewLine; super&period;onPreExecute&lpar;&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Perform pre-adding operation here&period;<&sol;span><&sol;strong>&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; protected List&lt&semi;Event&gt&semi; doInBackground&lpar;Object&period;&period;&period; params&rpar; &lbrace;&NewLine;&NewLine; Boolean shouldUpdate &equals; &lpar;Boolean&rpar; params&lbrack;0&rsqb;&semi;&NewLine; int position &equals; &lpar;int&rpar; params&lbrack;1&rsqb;&semi;&NewLine; String title &equals; &lpar;String&rpar; params&lbrack;2&rsqb;&semi;&NewLine; String detail &equals; &lpar;String&rpar; params&lbrack;3&rsqb;&semi;&NewLine; String date &equals; &lpar;String&rpar; params&lbrack;4&rsqb;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;check whether to add add or update event<&sol;span><&sol;strong>&NewLine; if &lpar;shouldUpdate &excl;&equals; null&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;update event<&sol;span><&sol;strong>&NewLine; if &lpar;shouldUpdate&rpar; &lbrace;&NewLine; Event event &equals; events&period;get&lpar;position&rpar;&semi;&NewLine; event&period;setTitle&lpar;title&rpar;&semi;&NewLine; event&period;setDescription&lpar;detail&rpar;&semi;&NewLine; event&period;setDate&lpar;date&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;update event into the database<&sol;strong><&sol;span>&NewLine; EventDatabase&period;getEventDatabase&lpar;getApplicationContext&lpar;&rpar;&rpar;&period;eventDao&lpar;&rpar;&period;updateEvent&lpar;event&rpar;&semi;&NewLine;&NewLine; &rcub; else &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;add event<&sol;span><&sol;strong>&NewLine; Event event &equals; new Event&lpar;&rpar;&semi;&NewLine; event&period;setTitle&lpar;title&rpar;&semi;&NewLine; event&period;setDescription&lpar;detail&rpar;&semi;&NewLine; event&period;setDate&lpar;date&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;add event into the database<&sol;span><&sol;strong>&NewLine; EventDatabase&period;getEventDatabase&lpar;getApplicationContext&lpar;&rpar;&rpar;&period;eventDao&lpar;&rpar;&period;addEvent&lpar;event&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; &rcub; else &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;delete event<&sol;span><&sol;strong>&NewLine; if &lpar;position &excl;&equals; -1&rpar; &lbrace;&NewLine; Event event &equals; events&period;get&lpar;position&rpar;&semi;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;delete event from database<&sol;span><&sol;strong>&NewLine; EventDatabase&period;getEventDatabase&lpar;getApplicationContext&lpar;&rpar;&rpar;&period;eventDao&lpar;&rpar;&period;deleteEvent&lpar;event&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;get events from database<&sol;strong><&sol;span>&NewLine; List&lt&semi;Event&gt&semi; events &equals; EventDatabase&period;getEventDatabase&lpar;getApplicationContext&lpar;&rpar;&rpar;&period;eventDao&lpar;&rpar;&period;getEvents&lpar;&rpar;&semi;&NewLine; return events&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; &commat;Override&NewLine; protected void onPostExecute&lpar;List&lt&semi;Event&gt&semi; items&rpar; &lbrace;&NewLine;&NewLine;<strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;get list of events from doInBackground&lpar;&rpar;<&sol;span><&sol;strong>&NewLine; events &equals; items&semi;&NewLine;&NewLine; adapter &equals; new EventsAdapter&lpar;events&comma; getApplicationContext&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine; recyclerView&period;setAdapter&lpar;adapter&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;shows NO EVENTS FOUND when list is empty<&sol;strong><&sol;span>&NewLine; checkListEmptyOrNot&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; private void showActionsDialog&lpar;final int position&rpar; &lbrace;&NewLine; CharSequence colors&lbrack;&rsqb; &equals; new CharSequence&lbrack;&rsqb;&lbrace;"Edit"&comma; "Delete"&rcub;&semi;&NewLine;&NewLine; AlertDialog&period;Builder builder &equals; new AlertDialog&period;Builder&lpar;this&rpar;&semi;&NewLine; builder&period;setTitle&lpar;"Choose option"&rpar;&semi;&NewLine; builder&period;setItems&lpar;colors&comma; new DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;DialogInterface dialog&comma; int which&rpar; &lbrace;&NewLine; if &lpar;which &equals;&equals; 0&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;show Alertdialog to update the event<&sol;span><&sol;strong>&NewLine; showEventDialog&lpar;true&comma; events&period;get&lpar;position&rpar;&comma; position&rpar;&semi;&NewLine; &rcub; else &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;delete event from database<&sol;span><&sol;strong>&NewLine; deleteEvent&lpar;position&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine; builder&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private void deleteEvent&lpar;int position&rpar; &lbrace;&NewLine;&NewLine; new DatabaseAsync&lpar;&rpar;&period;execute&lpar;null&comma; position&comma; null&comma; null&comma; null&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; private void showEventDialog&lpar;final Boolean shouldUpdate&comma; final Event event&comma; final int position&rpar; &lbrace;&NewLine;&NewLine; LayoutInflater layoutInflater &equals; LayoutInflater&period;from&lpar;getApplicationContext&lpar;&rpar;&rpar;&semi;&NewLine; View view &equals; layoutInflater&period;inflate&lpar;R&period;layout&period;event&lowbar;dialog&comma; null&rpar;&semi;&NewLine;&NewLine; AlertDialog&period;Builder alertDialogBuilderUserInput &equals; new AlertDialog&period;Builder&lpar;this&rpar;&semi;&NewLine; alertDialogBuilderUserInput&period;setView&lpar;view&rpar;&semi;&NewLine;&NewLine; TextView dialog&lowbar;title &equals; &lpar;TextView&rpar; view&period;findViewById&lpar;R&period;id&period;dialog&lowbar;title&rpar;&semi;&NewLine;&NewLine; final EditText edt&lowbar;title &equals; &lpar;EditText&rpar; view&period;findViewById&lpar;R&period;id&period;edt&lowbar;title&rpar;&semi;&NewLine;&NewLine; final EditText edt&lowbar;discription &equals; &lpar;EditText&rpar; view&period;findViewById&lpar;R&period;id&period;edt&lowbar;discription&rpar;&semi;&NewLine;&NewLine; tv&lowbar;date &equals; &lpar;TextView&rpar; view&period;findViewById&lpar;R&period;id&period;tv&lowbar;date&rpar;&semi;&NewLine;&NewLine; Button btn&lowbar;setdate &equals; &lpar;Button&rpar; view&period;findViewById&lpar;R&period;id&period;btn&lowbar;setdate&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;add listener to button to open datepickerdialog<&sol;strong><&sol;span>&NewLine; btn&lowbar;setdate&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;open datepickerdialog<&sol;strong><&sol;span>&NewLine; showDateDialog&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine; dialog&lowbar;title&period;setText&lpar;&excl;shouldUpdate &quest; "New Event" &colon; "Edit Event"&rpar;&semi;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol;in case of update we want all the&NewLine; &sol;&sol;fields to be set bydefault with text<&sol;strong><&sol;span>&NewLine; if &lpar;shouldUpdate &amp&semi;&amp&semi; event &excl;&equals; null&rpar; &lbrace;&NewLine; edt&lowbar;title&period;setText&lpar;event&period;getTitle&lpar;&rpar;&rpar;&semi;&NewLine; edt&lowbar;discription&period;setText&lpar;event&period;getDescription&lpar;&rpar;&rpar;&semi;&NewLine; tv&lowbar;date&period;setText&lpar;event&period;getDate&lpar;&rpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; alertDialogBuilderUserInput&NewLine; &period;setCancelable&lpar;false&rpar;&NewLine; &period;setPositiveButton&lpar;shouldUpdate &quest; "update" &colon; "add"&comma; new DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; public void onClick&lpar;DialogInterface dialogBox&comma; int id&rpar; &lbrace;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&rpar;&NewLine; &period;setNegativeButton&lpar;"cancel"&comma;&NewLine; new DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; public void onClick&lpar;DialogInterface dialogBox&comma; int id&rpar; &lbrace;&NewLine; dialogBox&period;cancel&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine; final AlertDialog alertDialog &equals; alertDialogBuilderUserInput&period;create&lpar;&rpar;&semi;&NewLine;&NewLine; alertDialog&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; alertDialog&period;getButton&lpar;AlertDialog&period;BUTTON&lowbar;POSITIVE&rpar;&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View v&rpar; &lbrace;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Show toast message when no text is entered<&sol;strong><&sol;span>&NewLine; if &lpar;TextUtils&period;isEmpty&lpar;edt&lowbar;title&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar; &amp&semi;&amp&semi; &excl;TextUtils&period;isEmpty&lpar;edt&lowbar;discription&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;MainActivity&period;this&comma; "Enter title&excl;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; else if &lpar;&excl;TextUtils&period;isEmpty&lpar;edt&lowbar;title&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar; &amp&semi;&amp&semi; TextUtils&period;isEmpty&lpar;edt&lowbar;discription&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;MainActivity&period;this&comma; "Enter description&excl;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; else if &lpar;TextUtils&period;isEmpty&lpar;edt&lowbar;title&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar; &amp&semi;&amp&semi; TextUtils&period;isEmpty&lpar;edt&lowbar;discription&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar;&rpar; &lbrace;&NewLine; Toast&period;makeText&lpar;MainActivity&period;this&comma; "Enter title and description&excl;"&comma; Toast&period;LENGTH&lowbar;SHORT&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; else &lbrace;&NewLine; alertDialog&period;dismiss&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;Update or add data into the database only when both field are filled&lpar;i&period;e title&comma;description&rpar;<&sol;span><&sol;strong>&NewLine; if &lpar;&excl;TextUtils&period;isEmpty&lpar;edt&lowbar;title&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar; &amp&semi;&amp&semi; &excl;TextUtils&period;isEmpty&lpar;edt&lowbar;discription&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar;&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; check if user updating note<&sol;span><&sol;strong>&NewLine;&NewLine; if &lpar;shouldUpdate &amp&semi;&amp&semi; event &excl;&equals; null&rpar; &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; update event<&sol;strong><&sol;span>&NewLine; new DatabaseAsync&lpar;&rpar;&period;execute&lpar;shouldUpdate&comma; position&comma; edt&lowbar;title&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&comma; edt&lowbar;discription&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&comma; tv&lowbar;date&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub; else &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; create new event<&sol;strong><&sol;span>&NewLine; new DatabaseAsync&lpar;&rpar;&period;execute&lpar;shouldUpdate&comma; -1&comma; edt&lowbar;title&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&comma; edt&lowbar;discription&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&comma; tv&lowbar;date&period;getText&lpar;&rpar;&period;toString&lpar;&rpar;&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; public static class DatePickerFragment extends DialogFragment implements DatePickerDialog&period;OnDateSetListener &lbrace;&NewLine;&NewLine; &commat;Override&NewLine; public Dialog onCreateDialog&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; final Calendar c &equals; Calendar&period;getInstance&lpar;&rpar;&semi;&NewLine; int year &equals; c&period;get&lpar;Calendar&period;YEAR&rpar;&semi;&NewLine; int month &equals; c&period;get&lpar;Calendar&period;MONTH&rpar;&semi;&NewLine; int day &equals; c&period;get&lpar;Calendar&period;DAY&lowbar;OF&lowbar;MONTH&rpar;&semi;&NewLine;&NewLine; return new DatePickerDialog&lpar;getActivity&lpar;&rpar;&comma; this&comma; year&comma; month&comma; day&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void onDateSet&lpar;DatePicker view&comma; int year&comma; int month&comma; int day&rpar; &lbrace;&NewLine; String monthVal&comma; dayVal &equals; "" &plus; day&semi;&NewLine; if &lpar;&lpar;month &plus; 1&rpar; &lt&semi; 10&rpar; &lbrace;&NewLine; month &plus;&equals; 1&semi;&NewLine; monthVal &equals; "0" &plus; month&semi;&NewLine; &rcub; else &lbrace;&NewLine; month &plus;&equals; 1&semi;&NewLine; monthVal &equals; "" &plus; month&semi;&NewLine; &rcub;&NewLine; if &lpar;day &lt&semi; 10&rpar; &lbrace;&NewLine; dayVal &equals; "0" &plus; day&semi;&NewLine; &rcub;&NewLine; SimpleDateFormat simpledateformat &equals; new SimpleDateFormat&lpar;"EEEE"&rpar;&semi;&NewLine; Date date &equals; new Date&lpar;year&comma; month&comma; day - 3&rpar;&semi;&NewLine; String dayOfWeek &equals; simpledateformat&period;format&lpar;date&rpar;&semi;&NewLine;&NewLine;&NewLine; tv&lowbar;date&period;setText&lpar;dayOfWeek &plus; "&bsol;n" &plus; dayVal &plus; "-" &plus; monthVal &plus; "-" &plus; year&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; private void showDateDialog&lpar;&rpar; &lbrace;&NewLine; DialogFragment newFragment &equals; new DatePickerFragment&lpar;&rpar;&semi;&NewLine; newFragment&period;show&lpar;getSupportFragmentManager&lpar;&rpar;&comma; "datePicker"&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void checkListEmptyOrNot&lpar;&rpar; &lbrace;&NewLine; if &lpar;events&period;isEmpty&lpar;&rpar;&rpar;&NewLine; noEventsFound&period;setVisibility&lpar;View&period;VISIBLE&rpar;&semi;&NewLine; else&NewLine; noEventsFound&period;setVisibility&lpar;View&period;GONE&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;&NewLine;

Exit mobile version