Android
Data objects
คู่มือการใช้ Parse SDK สำหรับการจัดการ CRUD บน Android
12 นาที
crud การจัดการ parse objects ใน android บทนำ ในส่วนนี้เราจะสร้างแอปพลิเคชัน android ที่ทำการดำเนินการ crud พื้นฐาน crud ย่อมาจาก create read update delete เมื่อคุณเก็บข้อมูลบน parse มันจะถูกสร้างขึ้นรอบๆ parseobject และแต่ละตัวจะมีคู่คีย์ ค่า ของข้อมูลที่เข้ากันได้กับ json ค่าที่คุณสามารถเก็บในฐานข้อมูล back4app อาจอยู่ในประเภทของ string, number, bool, array, object, date, file, pointer, relation และ null คุณสามารถดูข้อมูลเพิ่มเติมเกี่ยวกับประเภทข้อมูลได้โดยการคลิก ที่นี่ บทเรียนนี้ใช้แอปพื้นฐานที่สร้างขึ้นใน 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 repositories ของเรา kotlin ตัวอย่าง repository java ตัวอย่าง repository เป้าหมาย นี่คือภาพรวมของสิ่งที่เราจะทำให้สำเร็จ ข้อกำหนดเบื้องต้น ในการทำตามบทเรียนนี้ เราต้องการ android studio แอปที่สร้างขึ้นบน back4app หมายเหตุ ติดตาม บทเรียนการสร้าง parse app ใหม่ เพื่อเรียนรู้วิธีการสร้าง parse app บน back4app แอป android ที่เชื่อมต่อกับ back4app หมายเหตุ ติดตาม บทเรียนการติดตั้ง parse sdk เพื่อสร้างโปรเจกต์ android studio ที่เชื่อมต่อกับ back4app อุปกรณ์ (หรือ อุปกรณ์เสมือน ) ที่รัน android 4 1 (jelly bean) หรือใหม่กว่า การทำความเข้าใจแอป todo ของเรา เพื่อให้เข้าใจ parse บน android ได้ดีขึ้น คุณจะเห็นการดำเนินการ crud ที่นำไปใช้ในแอป todo แอปพลิเคชันจะมีอินเทอร์เฟซที่เรียบง่าย โดยมีช่องกรอกชื่อเรื่องและคำอธิบายเพื่อบันทึกงาน และรายการของงานที่ลงทะเบียนแล้ว คุณสามารถอัปเดตชื่อเรื่องหรือคำอธิบายของแต่ละงานได้ หมายเหตุ ในบทเรียนนี้เราจะสร้างกล่องโต้ตอบแจ้งเตือนที่กำหนดเอง ดังนั้น โค้ดจะถูกกำหนดให้ตามเทมเพลต คุณสามารถปรับให้เข้ากับการออกแบบของคุณเอง หากคุณมีปัญหาใด ๆ ให้ตรวจสอบที่เก็บ github สำหรับ kotlin และ java เริ่มกันเถอะ! ตามขั้นตอนถัดไป คุณจะสามารถสร้างแอป todo ที่จะเก็บงานต่างๆ ไว้ที่ฐานข้อมูล back4app 1 สร้างเทมเพลต todo app ไปที่ 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 เป็นพารามิเตอร์ไปยังฟังก์ชันลบที่ 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 }) เสร็จเรียบร้อย! ในจุดนี้ คุณได้เรียนรู้วิธีการทำการดำเนินการ crud พื้นฐานกับ parse บน android