Android
Data objects
在Android中实现Parse SDK进行CRUD操作的最佳实践
12 分
在android中进行crud解析对象 介绍 在本节中,我们将构建一个执行基本crud操作的android应用程序。crud是创建 读取 更新 删除的缩写。当您在parse上存储数据时,它是围绕parseobject构建的,每个对象包含json兼容数据的键值对。 您可以在back4app数据库中存储的值可以是字符串、数字、布尔值、数组、对象、日期、文件、指针、关系和null类型。 您可以通过点击 https //docs parseplatform org/js/guide/#data types 获取有关数据类型的更多信息。 本教程使用在android studio 4 1 1中创建的基本应用程序,使用 buildtoolsversion=30 0 2 buildtoolsversion=30 0 2 , compile sdk version = 30 0 2 compile sdk version = 30 0 2 和 targetsdkversion 30 targetsdkversion 30 在任何时候,您都可以通过我们的github存储库访问完整的项目。 https //github com/templates back4app/android crud operations kotlin https //github com/templates back4app/android crud operations java 目标 以下是我们将要实现的内容的预览: 前提条件 要完成本教程,我们需要: https //developer android com/studio/index html 在 back4app 上创建的应用程序。 注意: 请遵循 https //www back4app com/docs/get started/new parse app 了解如何在 back4app 上创建 parse 应用。 一个连接到 back4app 的安卓应用。 注意: 请遵循 https //www back4app com/docs/android/parse android sdk 以创建一个连接到 back4app 的 android studio 项目。 一台运行 android 4 1 (jelly bean) 或更高版本的设备(或 https //developer android com/studio/run/managing avds html )。 了解我们的待办事项应用 为了更好地理解android上的parse,您将看到在一个待办事项应用程序中实现的crud操作。该应用程序将具有一个简单的界面,包含一个标题和描述文本字段以注册任务,以及一个已注册任务的列表。您可以更新每个任务的标题或描述。 注意: 在本教程中,我们将制作自己的自定义警报对话框。因此,代码将被配置为遵循模板。您可以根据自己的设计进行调整。 如果您遇到任何问题,请查看github上的存储库, https //github com/templates back4app/android crud operations kotlin 和 https //github com/templates back4app/android crud operations java 让我们开始吧! 按照接下来的步骤,您将能够构建一个 todo 应用程序,该应用程序将在 back4app 数据库中存储任务。 1 创建 todo 应用程序模板 去 android studio,找到 activity main xml activity main xml 布局文件,路径为 (project/app/res/layout/activity main xml) (project/app/res/layout/activity main xml) ,然后用你自己的代码替换下面的代码。这个 xml 代码将是我们的 mainactivity 的 mainactivity 的 设计,我们将把这些视图绑定到我们的 mainactivity java mainactivity java 类并使用它们。 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 创建对象 创建函数将创建一个具有标题和描述的新任务。 要添加一个todo,我们在屏幕上出现的弹出窗口中输入标题和描述值,将其设置为parseobject并保存该对象。我们通过使用parse提供的函数将该对象保存在数据库中。 注意: 在这个项目中,我们将制作自己的自定义警报对话框。您可以根据自己的需要设计警报对话框,并将其设置为您稍后将使用的对话框视图。 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 读取对象 使用 parse 提供的函数,我们可以将一个类中的所有数据作为 parseobject 列表获取。我们创建一个如下所示的查询,并获取 todo 类中的所有数据。 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 } 在这个函数中,我们将返回的列表作为参数传递给我们的 适配器 适配器 , 设置这个 适配器 适配器 , 到我们的 recyclerview recyclerview , 并将此列表中每个对象的值打印到其各自的视图中。如果列表没有项目,我们将设置我们的 empty text empty text , 视图为 可见 可见 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 更新对象 通过我们适配器视图中的编辑按钮,我们正在执行更新。通过 mutablelivedata mutablelivedata , android 提供的对象,我们可以监听主页上编辑按钮的点击事件。当点击此按钮时,我们打开相同的弹出窗口,这次我们用我们点击的对象的标题和描述值填充这个弹出窗口。然后,当用户更改此描述和标题并按下保存时,首先我们根据该对象的 id 从数据库中获取数据,然后设置新变量并再次保存对象。 注意: mutablelivedata 是 livedata 的子类,用于其某些属性(setvalue/postvalue),使用这些属性我们可以轻松通知 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 删除对象 我们在适配器的视图中监听删除按钮的点击事件,使用来自主页的 mutablelivedata mutablelivedata 对象,就像编辑按钮一样。当点击删除按钮时,我们将 parseobject 的对象 id 作为参数传递给 parse 提供的删除函数,并从数据库中删除该对象。 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 }) 完成! 到此为止,您已经学习了如何在 android 上使用 parse 进行基本的 crud 操作。