Android
Data objects
Basic Operations
13 min
crud parse objects in android introduction in this section we are gonna build an android application that performs basic crud operations crud is abbreviation of create read update delete when you store data on parse it is built around parseobject and each one contains key value pairs of json compatible data the values you can store in back4app database could be in type of string, number, bool, array, object, date, file, pointer, relation and null you can get more information about data types by clicking here this tutorial uses a basic app created in android studio 4 1 1 with buildtoolsversion=30 0 2 buildtoolsversion=30 0 2 , compile sdk version = 30 0 2 compile sdk version = 30 0 2 and targetsdkversion 30 targetsdkversion 30 at any time, you can access the complete project via our github repositories kotlin example repository java example repository goal here is a preview of what we are gonna achive prerequisites to complete this tutorial, we need android studio an app created on back4app note follow the new parse app tutorial to learn how to create a parse app on back4app an android app connected to back4app note follow the install parse sdk tutoria l to create an android studio project connected to back4app a device (or virtual device ) running android 4 1 (jelly bean) or newer understanding our todo app to better understand parse on android, you will see the crud operations implemented on a todo app the application will have a simple interface, with a title and description text field to register a task and a list of registered tasks you can update each task’s title or description note in this tutorial we will make our own custom alert dialog therefore, the codes will be configured to follow the template you can adjust it to your own design if you have any trouble, check the github repositories for kotlin and java let’s get started! following the next steps, you will be able to build a todo app that will store the tasks at back4app database 1 create todo app template go to android studio, and find activity main xml activity main xml layout file from the project (project/app/res/layout/activity main xml) (project/app/res/layout/activity main xml) and then replace the code below with your own code this xml code will be our mainactivity's mainactivity's design, and we will bind this views to our mainactivity java mainactivity java class and use them 1 \<?xml version="1 0" encoding="utf 8"?> 2 \<androidx constraintlayout widget constraintlayout xmlns\ android="http //schemas android com/apk/res/android" 3 xmlns\ app="http //schemas android com/apk/res auto" 4 xmlns\ tools="http //schemas android com/tools" 5 android\ layout width="match parent" 6 android\ layout height="match parent" 7 tools\ context=" mainactivity"> 8 9 \<! title >> 10 \<textview 11 android\ id="@+id/textview" 12 android\ layout width="0dp" 13 android\ layout height="wrap content" 14 android\ background="@color/blue 700" 15 android\ gravity="center horizontal" 16 android\ padding="24dp" 17 android\ text="todo list" 18 android\ textcolor="@color/white" 19 app\ layout constraintend toendof="parent" 20 app\ layout constraintstart tostartof="parent" 21 app\ layout constrainttop totopof="parent" /> 22 23 \<! we will open a pop up view when clicked to this button >> 24 \<com google android material floatingactionbutton floatingactionbutton 25 android\ id="@+id/fab" 26 style="@style/widget materialcomponents floatingactionbutton" 27 android\ layout width="wrap content" 28 android\ layout height="wrap content" 29 android\ layout margin="16dp" 30 android\ scaletype="centerinside" 31 android\ src="@drawable/ic baseline add 24" 32 app\ backgroundtint="@color/blue 700" 33 app\ fabsize="normal" 34 app\ layout constraintbottom tobottomof="parent" 35 app\ layout constraintend toendof="parent" 36 app\ tint="@color/white" /> 37 38 \<! we will show this text view when data list is empty >> 39 \<textview 40 android\ id="@+id/empty text" 41 android\ layout width="wrap content" 42 android\ layout height="wrap content" 43 android\ text="list is empty" 44 android\ layout margintop="32dp" 45 android\ textsize="20sp" 46 android\ visibility="gone" 47 app\ layout constraintend toendof="parent" 48 app\ layout constraintstart tostartof="parent" 49 app\ layout constrainttop tobottomof="@+id/textview" /> 50 51 \<! we will adapt the data list to this recyclerview > 52 \<androidx recyclerview\ widget recyclerview 53 android\ id="@+id/recyclerview" 54 android\ layout width="0dp" 55 android\ layout height="0dp" 56 app\ layout constraintbottom tobottomof="parent" 57 app\ layout constraintend toendof="parent" 58 app\ layout constraintstart tostartof="parent" 59 app\ layout constrainttop tobottomof="@+id/textview"> 60 \</androidx recyclerview\ widget recyclerview> 61 \</androidx constraintlayout widget constraintlayout> 2 create object the create function will create a new task with the title and description to add a todo, we enter the title and description values in the pop up that appears on the screen, set it to the parseobject and save this object we save this object in the database by using the function that parse has provided us note in this project we will make our own custom alert dialog you can design your alert dialog as you want and set it to the view of the dialog you will use later 1 openinputpopupdialogbutton setonclicklistener(fabbuttonview > { 2 alertdialog builder alertdialogbuilder = new alertdialog builder(mainactivity this); 3 alertdialogbuilder settitle("create a todo"); 4 alertdialogbuilder setcancelable(true); 5 initpopupviewcontrols(); 6 //we are setting our custom popup view by alertdialog builder 7 alertdialogbuilder setview(popupinputdialogview); 8 final alertdialog alertdialog = alertdialogbuilder create(); 9 alertdialog show(); 10 savetodobutton setonclicklistener(savebuttonview > savetodo(alertdialog)); 11 canceluserdatabutton setonclicklistener(cancelbuttonview > alertdialog cancel()); 12 }); 13 14 //this is our savetodo function 15 private void savetodo(alertdialog alertdialog) { 16 parseobject todo = new parseobject("todo"); 17 if (titleinput gettext() tostring() length() != 0 && descriptioninput gettext() tostring() length() != 0) { 18 alertdialog cancel(); 19 progressdialog show(); 20 todo put("title", titleinput gettext() tostring()); 21 todo put("description", descriptioninput gettext() tostring()); 22 todo saveinbackground(e > { 23 progressdialog dismiss(); 24 if (e == null) { 25 //we saved the object and fetching data again 26 gettodolist(); 27 } else { 28 //we have an error we are showing error message here 29 showalert("error", e getmessage()); 30 } 31 }); 32 } else { 33 showalert("error", "please enter a title and description"); 34 } 35 }1 openinputpopupdialogbutton? setonclicklistener { fabbuttonview > 2 val alertdialogbuilder = alertdialog builder(this\@mainactivity) 3 alertdialogbuilder settitle("create a todo") 4 alertdialogbuilder setcancelable(true) 5 initpopupviewcontrols() 6 //we are setting our custom popup view by alertdialog builder 7 alertdialogbuilder setview(popupinputdialogview) 8 val alertdialog = alertdialogbuilder create() 9 alertdialog show() 10 savetodobutton? setonclicklistener { savebuttonview > 11 savedata(alertdialog) 12 } 13 canceluserdatabutton? setonclicklistener { cancelbuttonview > 14 alertdialog cancel() 15 } 16 } 17 18 //this is our savetodo function 19 private fun savedata(alertdialog alertdialog) { 20 val todo = parseobject("todo") 21 if (titleinput? text tostring() isnotempty() && descriptioninput? text tostring() isnotempty()) { 22 alertdialog cancel() 23 progressdialog? show() 24 todo put("title", titleinput? text tostring()) 25 todo put("description", descriptioninput? text tostring()) 26 todo saveinbackground { e > 27 progressdialog? dismiss() 28 if (e == null) { 29 //we saved the object and fetching data again 30 gettodolist() 31 } else { 32 //we have an error we are showing error message here 33 showalert("error", e message!!) 34 } 35 } 36 } else { 37 showalert("error", "please enter a title and description") 38 } 39 } 3 read object with the function that parse has provided us, we can fetch all the data in a class as parseobject list we create a query like the one below and fetch all the data in the todo class 1 private void gettodolist() { 2 progressdialog show(); 3 parsequery\<parseobject> query = parsequery getquery("todo"); 4 query orderbydescending("createdat"); 5 query findinbackground((objects, e) > { 6 progressdialog dismiss(); 7 if (e == null) { 8 //we are initializing todo object list to our adapter 9 inittodolist(objects); 10 } else { 11 showalert("error", e getmessage()); 12 } 13 }); 14 }1 private fun gettodolist() { 2 progressdialog? show() 3 val query = parsequery getquery\<parseobject>("todo") 4 query orderbydescending("createdat") 5 query findinbackground { objects, e > 6 progressdialog? dismiss() 7 if (e == null) { 8 //we are initializing todo object list to our adapter 9 inittodolist(objects) 10 } else { 11 showalert("error", e message!!) 12 } 13 } 14 } in this function, we give the returned list as a parameter to our adapter adapter , set this adapter adapter , to our recyclerview recyclerview , and we print the values of each object of this list into its own views and if list has no item, we are setting our empty text empty text , view’s as visible visible 1 private void inittodolist(list\<parseobject> list) { 2 if (list == null || list isempty()) { 3 empty text setvisibility(view\ visible); 4 return; 5 } 6 empty text setvisibility(view\ gone); 7 8 todoadapter adapter = new todoadapter(list, this); 9 10 recyclerview\ setlayoutmanager(new linearlayoutmanager(this)); 11 recyclerview\ setadapter(adapter); 12 }1 private fun inittodolist(list list\<parseobject>?) { 2 if (list == null || list isempty()) { 3 empty text!! visibility = view\ visible 4 return 5 } 6 empty text? visibility = view\ gone 7 8 val adapter = todoadapter(list as arraylist\<parseobject>, this) 9 10 recyclerview? layoutmanager = linearlayoutmanager(this) 11 recyclerview? adapter = adapter 12 } 4 update object with the edit button in the view of our adapter, we are performing our updates with the mutablelivedata mutablelivedata , object provided by android, we can listen to the click event of the edit button on our home page when this button is clicked, we open the same pop up and this time we populate this pop up with the title and description values of the object we clicked then, when the user changes this description and title and press save, first we fetch from the database with the id of this object, then we set the new variables and save object again note mutablelivedata is a subclass of livedata which is used for some of it’s properties (setvalue/postvalue) and using these properties we can easily notify the ui 1 adapter oneditlistener observe(this, parseobject > { 2 alertdialog builder alertdialogbuilder = new alertdialog builder(mainactivity this); 3 alertdialogbuilder settitle("update a todo"); 4 alertdialogbuilder setcancelable(true); 5 //we are initializing popup views with title and description parameters of parseobject 6 initpopupviewcontrols(parseobject getstring("title"), parseobject getstring("description")); 7 alertdialogbuilder setview(popupinputdialogview); 8 final alertdialog alertdialog = alertdialogbuilder create(); 9 alertdialog show(); 10 savetodobutton setonclicklistener(savetodobuttonview > { 11 if (titleinput gettext() tostring() length() != 0 && descriptioninput gettext() tostring() length() != 0) { 12 alertdialog cancel(); 13 progressdialog show(); 14 parseobject put("title", titleinput gettext() tostring()); 15 parseobject put("description", descriptioninput gettext() tostring()); 16 parseobject saveinbackground(e1 > { 17 progressdialog dismiss(); 18 if (e1 == null) { 19 gettodolist(); 20 } else { 21 showalert("error", e1 getmessage()); 22 } 23 }); 24 } else { 25 showalert("error", "please enter a title and description"); 26 } 27 }); 28 canceluserdatabutton setonclicklistener(cancelbuttonview > alertdialog cancel()); 29 });1 adapter clicklistenertoedit observe(this\@mainactivity, { parseobject > 2 val alertdialogbuilder = alertdialog builder(this\@mainactivity) 3 alertdialogbuilder settitle("update a todo") 4 alertdialogbuilder setcancelable(true) 5 6 //we are initializing popup views with title and description parameters of parseobject 7 8 initpopupviewcontrols( 9 parseobject getstring("title")!!, 10 parseobject getstring("description")!! 11 ) 12 13 alertdialogbuilder setview(popupinputdialogview) 14 val alertdialog = alertdialogbuilder create() 15 alertdialog show() 16 17 savetodobutton? setonclicklistener { savebuttonview > 18 if (titleinput? text tostring() isnotempty() && descriptioninput? text tostring() isnotempty()) { 19 alertdialog cancel() 20 progressdialog? show() 21 parseobject put("title", titleinput? text tostring()) 22 parseobject put("description", descriptioninput? text tostring()) 23 parseobject saveinbackground { e1 > 24 progressdialog? dismiss() 25 if (e1 == null) { 26 gettodolist() 27 } else { 28 showalert("error", e1 message!!) 29 } 30 } 31 } else { 32 showalert("error", "please enter a title and description") 33 } 34 } 35 }) 5 delete object we listen to the click event of the delete button in the view of our adapter with the mutablelivedata mutablelivedata object from the home page, as in the edit button when the delete button is clicked, we give the object id of the parseobject as a parameter to the delete function that parse has provided us and delete this object from the database 1 adapter ondeletelistener observe(this, parseobject > { 2 progressdialog show(); 3 parseobject deleteinbackground(e > { 4 progressdialog dismiss(); 5 if (e == null) { 6 //we deleted the object and fetching data again 7 gettodolist(); 8 } else { 9 showalert("error",e getmessage()); 10 } 11 }); 12 });1 adapter ondeletelistener observe(this\@mainactivity, { parseobject > 2 progressdialog? show() 3 parseobject deleteinbackground { e > 4 progressdialog? dismiss() 5 if (e == null) { 6 //we deleted the object and fetching data again 7 gettodolist() 8 } else { 9 showalert("error", e message!!) 10 } 11 } 12 }) it’s done! at this point, you have learned how to do the basic crud operations with parse on android