Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me with this Android App. Thank You! I have this Android App that creates a To-Do List using the Room Persistance Library. How

Please help me with this Android App. Thank You!

I have this Android App that creates a To-Do List using the Room Persistance Library.

How could I modify it to add timestamps to the items in the list and also add a menu item to sort the items from oldest to newest and also an option to sort from newest to oldest?

This is the code:

MainActivity.java:

import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; import android.arch.persistence.room.PrimaryKey; @Entity(tableName = "tasks") public class Task { @PrimaryKey(autoGenerate = true) private int mId; @ColumnInfo(name = "title") private String mTitle; public Task(String title){ mTitle = title; } public int getId(){ return mId; } public void setId(int id){ mId = id; } public String getTitle() { return mTitle; } public void setTitle(String title){ mTitle = title; } @Override public String toString(){ return mTitle; } } 

Task.java:

import android.arch.persistence.room.ColumnInfo; import android.arch.persistence.room.Entity; import android.arch.persistence.room.PrimaryKey; @Entity(tableName = "tasks") public class Task { @PrimaryKey(autoGenerate = true) private int mId; @ColumnInfo(name = "title") private String mTitle; public Task(String title){ mTitle = title; } public int getId(){ return mId; } public void setId(int id){ mId = id; } public String getTitle() { return mTitle; } public void setTitle(String title){ mTitle = title; } @Override public String toString(){ return mTitle; } } 

TaskDao.java:

import android.arch.persistence.room.Dao; import android.arch.persistence.room.Insert; import android.arch.persistence.room.Query; import java.util.List; @Dao public interface TaskDao { @Insert public void addTask(Task task); @Query("SELECT * FROM tasks") public List getAllTasks(); @Query("DELETE FROM tasks WHERE title = :taskString") public void deleteByTitle(String taskString); } 

ToDoDb.java:

import android.arch.persistence.room.Database; import android.arch.persistence.room.Room; import android.arch.persistence.room.RoomDatabase; import android.content.Context; @Database(entities = {Task.class}, version = 1, exportSchema = false) public abstract class ToDoDb extends RoomDatabase{ private static ToDoDb sInstance; public abstract TaskDao task(); public static synchronized ToDoDb getInstance(Context context){ if(sInstance == null){ sInstance = Room.databaseBuilder(context.getApplicationContext(), ToDoDb.class, "todo.db") .allowMainThreadQueries() .fallbackToDestructiveMigration() .build(); } return sInstance; } } 

These are the layouts within the 'layout' folder which is within the 'res' folder.

res/layout/activity_main.xml:

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="match_parent"  android:layout_marginLeft="10dp"  android:layout_marginRight="10dp"  android:layout_marginTop="5dp"> <ListView  android:id="@+id/list_todo"  android:layout_width="wrap_content"  android:layout_height="wrap_content" /> RelativeLayout> 

res/layout/item_todo.xml:

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="match_parent"  android:layout_gravity="center_vertical"> <TextView  android:id="@+id/task_title"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:textSize="20sp"/> <Button  android:onClick="deleteTask"  android:id="@+id/task_delete"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_alignParentRight="true"  android:layout_alignParentEnd="true"  android:text="@string/done"/> RelativeLayout>

res/layout/task_dialog.xml:

xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="match_parent"  android:layout_height="match_parent"> <EditText  android:id="@+id/new_task"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:layout_marginTop="4dp"  android:layout_marginLeft="20dp"  android:layout_marginRight="20dp"  android:layout_marginBottom="4dp"  android:hint="Add New Task" /> LinearLayout>

There is also a 'menu' folder within the 'res' folder which contains 'main_menu.xml':

res/menu/main_menu.xml:

xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res-auto"> <item  android:id="@+id/action_add_task"  android:icon="@android:drawable/ic_menu_add"  android:title="Add Task"  app:showAsAction="always"/> menu>

This is the 'strings.xml' file which is inside the values folder within the 'res' folder:

res/values/strings.xml:

<resources> <string name="app_name">To-Do Liststring> <string name="add_new_task">add_new_taskstring> <string name="done">Donestring> <string name="dialog_title">Add a new taskstring> <string name="dialog_message">What do you want to do next?string> <string name="add">Addstring> <string name="cancel">cancelstring> resources> 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning PostgreSQL On The Cloud Simplifying Database As A Service On Cloud Platforms

Authors: Baji Shaik ,Avinash Vallarapu

1st Edition

1484234464, 978-1484234464

More Books

Students also viewed these Databases questions

Question

What values do businesses need?

Answered: 1 week ago