การสืบค้นข้อมูลพื้นฐานใน Android ด้วย Parse
11 นาที
คำค้นพื้นฐานบน android บทนำ ในคู่มือนี้ คุณจะทำการค้นหาข้อมูลพื้นฐานใน \<font color="#2166ae">parse\</font> และสร้างแอป \<font color="#2166ae">android\</font> โดยใช้การค้นหาเหล่านี้ คุณจะได้เรียนรู้วิธีการตั้งค่าและค้นหาข้อมูลที่สมจริงโดยใช้ \<font color="#2166ae">back4app\</font> และ \<font color="#2166ae">android\</font> บทเรียนนี้ใช้แอปที่สร้างขึ้นใน android studio 4 1 1 โดยมี \<font color="#2166ae">buildtoolsversion=30 0 2\</font> , \<font color="#2166ae">compile sdk version = 30 0 2\</font> และ \<font color="#2166ae">targetsdkversion=30\</font> ในทุกช่วงเวลา คุณสามารถเข้าถึงโปรเจกต์ android ที่สร้างขึ้นจากบทเรียนนี้ได้ที่ที่เก็บ github ของเรา ที่เก็บตัวอย่าง kotlin https //github com/templates back4app/android parse sdk kotlin ที่เก็บตัวอย่าง java https //github com/templates back4app/android parse sdk java เป้าหมาย เป้าหมายของเราคือการค้นหาข้อมูลที่เก็บไว้ใน \<font color="#2166ae">back4app\</font> จากแอป \<font color="#2166ae">android\</font> นี่คือภาพรวมของสิ่งที่เราจะทำให้สำเร็จ ข้อกำหนดเบื้องต้น ในการทำตามบทเรียนนี้ เราต้องการ android studio https //developer android com/studio/index html แอปที่สร้างขึ้นบน back4app หมายเหตุ ติดตาม บทเรียนการสร้าง parse app ใหม่ https //www back4app com/docs/get started/new parse app เพื่อเรียนรู้วิธีการสร้าง parse app บน back4app แอป android ที่เชื่อมต่อกับ back4app หมายเหตุ ติดตาม บทเรียนการติดตั้ง parse sdk https //www back4app com/docs/android/parse android sdk เพื่อสร้างโปรเจกต์ android studio ที่เชื่อมต่อกับ back4app อุปกรณ์ (หรือ อุปกรณ์เสมือน https //developer android com/studio/run/managing avds html ) ที่รัน android 4 1 (jelly bean) หรือใหม่กว่า เริ่มกันเลย! ก่อนที่จะดำเนินการต่อ เราจำเป็นต้องเชื่อมต่อ \<font color="#2166ae">back4app\</font> กับแอปพลิเคชันของเรา คุณควรบันทึก \<font color="#2166ae">appid\</font> และ \<font color="#2166ae">clientkey\</font> จาก \<font color="#2166ae">back4app\</font> ไปยัง \<font color="#2166ae">string xml\</font> ไฟล์และจากนั้นให้ init \<font color="#2166ae">parse\</font> ใน \<font color="#2166ae">app java\</font> หรือ \<font color="#2166ae">app kt\</font> ไฟล์。 ติดตาม บทเรียนการสร้างแอป parse ใหม่ https //www back4app com/docs/android/parse android sdk หากคุณไม่รู้ว่าจะ init \<font color="#2166ae">parse\</font> ไปยังแอปของคุณอย่างไร หรือคุณสามารถดาวน์โหลดโปรเจกต์ที่เราแชร์ลิงก์ github ข้างต้นและแก้ไขเฉพาะ \<font color="#2166ae">appid\</font> และ \<font color="#2166ae">clientkey\</font> ตามที่คุณต้องการ 1 การเข้าใจคลาส parse query การดำเนินการค้นหา parse ใด ๆ จะใช้ \<font color="#2166ae">parsequery\</font> ประเภทวัตถุ ซึ่งจะช่วยให้คุณดึงข้อมูลเฉพาะจากฐานข้อมูลของคุณตลอดทั้งแอปของคุณ เป็นสิ่งสำคัญที่ต้องรู้ว่า \<font color="#2166ae">parsequery\</font> จะถูกแก้ไขเฉพาะหลังจากเรียกใช้วิธีการดึงข้อมูล (เช่น \<font color="#2166ae">query findinbackground\</font> ) ดังนั้นการค้นหาสามารถตั้งค่าและมีการปรับแต่งหลายอย่างที่สามารถเชื่อมโยงกันได้ก่อนที่จะถูกเรียกใช้งานจริง ในการสร้าง \<font color="#2166ae">parsequery\</font> , คุณต้องส่งพารามิเตอร์เป็น parseobject subclass ที่ต้องการ ซึ่งจะเป็นตัวที่เก็บผลลัพธ์ของการค้นหาของคุณ ตัวอย่างการค้นหาสามารถดูได้ด้านล่าง ซึ่งมีการค้นหา profile subclass ที่สมมติขึ้น 1 // this will create your query 2 parsequery\<parseobject> query = new parsequery<>("profile"); 3 // the query will resolve only after calling this method 4 query findinbackground();1 // this will create your query 2 val query = parsequery\<parseobject>("profile") 3 // the query will resolve only after calling this method 4 query findinbackground() คุณสามารถอ่านเพิ่มเติมเกี่ยวกับ \<font color="#2166ae">parse query\</font> คลาส ที่นี่ในเอกสารอย่างเป็นทางการ https //www back4app com/docs/javascript/parse javascript sdk 2 บันทึกข้อมูลบางอย่างใน back4app ในขั้นตอนนี้ เราจะสร้าง class ด้วย js console และโค้ด javascript ที่จัดเตรียมโดย \<font color="#2166ae">parse\</font> และเราจะสร้างการค้นหาสำหรับ class นี้ มาสร้าง \<font color="#2166ae">โปรไฟล์\</font> คลาส ซึ่งจะเป็นเป้าหมายของการสอบถามในคู่มือนี้ บน \<font color="#2166ae">parse\</font> แดชบอร์ด คอนโซล javascript สามารถรันโค้ด javascript ได้โดยตรง โดยการสอบถามและอัปเดตเนื้อหาฐานข้อมูลแอปพลิเคชันของคุณโดยใช้คำสั่ง js sdk รันโค้ดด้านล่างจากคอนโซล js ของคุณและแทรกข้อมูลลงใน back4app นี่คือรูปลักษณ์ของคอนโซล js ในแดชบอร์ดของคุณ ไปข้างหน้าและสร้างผู้ใช้ \<font color="#2166ae">โปรไฟล์\</font> คลาสด้วยเนื้อหาตัวอย่างต่อไปนี้ 1 // add profile objects and create table 2 // adam sandler 3 let profile = new parse object('profile'); 4 profile set('name', 'adam sandler'); 5 profile set('birthday', new date('09/09/1966')); 6 profile set('friendcount', 2); 7 profile set('favoritefoods', \['lobster', 'bread']); 8 await profile save(); 9 10 // adam levine 11 profile = new parse object('profile'); 12 profile set('name', 'adam levine'); 13 profile set('birthday', new date('03/18/1979')); 14 profile set('friendcount', 52); 15 profile set('favoritefoods', \['cake', 'bread']); 16 await profile save(); 17 18 // carson kressley 19 profile = new parse object('profile'); 20 profile set('name', 'carson kressley'); 21 profile set('birthday', new date('11/11/1969')); 22 profile set('friendcount', 12); 23 profile set('favoritefoods', \['fish', 'cookies']); 24 await profile save(); 25 26 // dan aykroyd 27 profile = new parse object('profile'); 28 profile set('name', 'dan aykroyd'); 29 profile set('birthday', new date('07/01/1952')); 30 profile set('friendcount', 66); 31 profile set('favoritefoods', \['jam', 'peanut butter']); 32 await profile save(); 33 34 // eddie murphy 35 profile = new parse object('profile'); 36 profile set('name', 'eddie murphy'); 37 profile set('birthday', new date('04/03/1961')); 38 profile set('friendcount', 49); 39 profile set('favoritefoods', \['lettuce', 'pepper']); 40 await profile save(); 41 42 // fergie 43 profile = new parse object('profile'); 44 profile set('name', 'fergie'); 45 profile set('birthday', new date('03/27/1975')); 46 profile set('friendcount', 55); 47 profile set('favoritefoods', \['lobster', 'shrimp']); 48 await profile save(); 49 50 console log('success!'); 3 สืบค้นข้อมูล ตอนนี้คุณมีคลาสที่มีข้อมูลแล้ว เราสามารถทำการสืบค้นพื้นฐานในนั้นได้ มาลองเริ่มต้นด้วยการกรอง \<font color="#2166ae">โปรไฟล์\</font> ผลลัพธ์ตามชื่อ ซึ่งเป็นฟิลด์ประเภทสตริง โดยค้นหาค่าที่มีชื่อ \<font color="#2166ae">อดัม\</font> โดยใช้ \<font color="#2166ae">parse query contains\</font> วิธีการ 1 // create your query 2 let parsequery = new parse query('profile'); 3 4 // `contains` is a basic query method that checks if string field 5 // contains a specific substring 6 parsequery contains('name', 'adam'); 7 8 // the query will resolve only after calling this method, retrieving 9 // an array of `parse objects` 10 let queryresults = await parsequery find(); 11 12 // let's show the results 13 for (let result of queryresults) { 14 // you access `parse objects` attributes by using ` get` 15 console log(result get('name')); 16 }; ตอนนี้เรามาทำการค้นหาตามฟิลด์ประเภทจำนวน \<font color="#2166ae">friendcount\</font> โดยใช้วิธีการค้นหาทั่วไปอีกวิธีหนึ่ง, \<font color="#2166ae">parse query greaterthan\</font> ในกรณีนี้ เราต้องการผู้ใช้ \<font color="#2166ae">profiles\</font> ที่จำนวนเพื่อนมากกว่า 20 1 // create your query 2 let parsequery = new parse query('profile'); 3 4 // `greaterthan` is a basic query method that does what it 5 // says on the tin 6 parsequery greaterthan('friendcount', 20); 7 8 // the query will resolve only after calling this method, retrieving 9 // an array of `parse objects` 10 let queryresults = await parsequery find(); 11 12 // let's show the results 13 for (let result of queryresults) { 14 // you access `parse objects` attributes by using ` get` 15 console log(`name ${result get('name')}, friend count ${result get('friendcount')}`); 16 }; วิธีการค้นหาอื่น ๆ ที่เกิดขึ้นซ้ำคือ \<font color="#2166ae">parse query ascending\</font> และ \<font color="#2166ae">parse query descending\</font> , ซึ่งรับผิดชอบในการจัดเรียงคำค้นของคุณ การจัดเรียงนี้สามารถทำได้ในประเภทข้อมูลส่วนใหญ่ ดังนั้นมาจัดเรียงคำค้นตามฟิลด์วันที่ \<font color="#2166ae">birthday\</font> โดยคนที่อายุน้อยที่สุด 1 // create your query 2 let parsequery = new parse query('profile'); 3 4 // `descending` and `ascending` can and should be chained 5 // with other query methods to improve your queries 6 parsequery descending('birthday'); 7 8 // the query will resolve only after calling this method, retrieving 9 // an array of `parse objects` 10 let queryresults = await parsequery find(); 11 12 // let's show the results 13 for (let result of queryresults) { 14 // you access `parse objects` attributes by using ` get` 15 console log(`name ${result get('name')}, birthday ${result get('birthday')}`); 16 }; ตามที่กล่าวไว้ที่นี่ก่อนหน้านี้ คุณสามารถและควรเชื่อมโยงวิธีการค้นหาเพื่อให้ได้ผลลัพธ์ที่ละเอียดมากขึ้น มารวมตัวอย่างก่อนหน้านี้ในคำขอค้นหาเดียวกัน 1 // create your query 2 let parsequery = new parse query('profile'); 3 4 parsequery contains('name', 'adam'); 5 parsequery greaterthan('friendcount', 20); 6 parsequery descending('birthday'); 7 8 // the query will resolve only after calling this method, retrieving 9 // an array of `parse objects` 10 let queryresults = await parsequery find(); 11 12 // let's show the results 13 for (let result of queryresults) { 14 // you access `parse objects` attributes by using ` get` 15 console log(`name ${result get('name')}, friend count ${result get('friendcount')}, birthday ${result get('birthday')}`); 16 }; 4 คำถามจากแอป android ของเรา เราจะทำการดำเนินการที่เราทำข้างต้นจาก js console ด้วย java และ kotlin ในแอป android ของเรา เราจะทำการแสดงโปรไฟล์โดยการทำคำถาม 4 แบบที่แตกต่างกัน 1 private void doquerybyname() { 2 parsequery\<parseobject> query = new parsequery<>("profile"); 3 query wherecontains("name", "adam"); 4 progressdialog show(); 5 query findinbackground((objects, e) > { 6 progressdialog hide(); 7 if (e == null) { 8 adapter = new resultadapter(this, objects); 9 resultlist setlayoutmanager(new linearlayoutmanager(this)); 10 resultlist setadapter(adapter); 11 } else { 12 toast maketext(this, e getlocalizedmessage(), toast length short) show(); 13 } 14 }); 15 } 16 17 private void doquerybyfriendcount() { 18 parsequery\<parseobject> query = new parsequery<>("profile"); 19 query wheregreaterthan("friendcount", 20); 20 progressdialog show(); 21 query findinbackground((objects, e) > { 22 progressdialog hide(); 23 if (e == null) { 24 adapter = new resultadapter(this, objects); 25 resultlist setlayoutmanager(new linearlayoutmanager(this)); 26 resultlist setadapter(adapter); 27 } else { 28 toast maketext(this, e getlocalizedmessage(), toast length short) show(); 29 } 30 }); 31 } 32 33 private void doquerybyordering() { 34 parsequery\<parseobject> query = new parsequery<>("profile"); 35 query orderbydescending("birthday"); 36 progressdialog show(); 37 query findinbackground((objects, e) > { 38 progressdialog hide(); 39 if (e == null) { 40 adapter = new resultadapter(this, objects); 41 resultlist setlayoutmanager(new linearlayoutmanager(this)); 42 resultlist setadapter(adapter); 43 } else { 44 toast maketext(this, e getlocalizedmessage(), toast length short) show(); 45 } 46 }); 47 } 48 49 private void doquerybyall() { 50 parsequery\<parseobject> query = new parsequery<>("profile"); 51 query wherecontains("name", "adam"); 52 query wheregreaterthan("friendcount", 20); 53 query orderbydescending("birthday"); 54 progressdialog show(); 55 56 query findinbackground((objects, e) > { 57 progressdialog hide(); 58 59 if (e == null) { 60 adapter = new resultadapter(this, objects); 61 resultlist setlayoutmanager(new linearlayoutmanager(this)); 62 resultlist setadapter(adapter); 63 resultlist setnestedscrollingenabled(false); 64 } else { 65 toast maketext(this, e getlocalizedmessage(), toast length short) show(); 66 } 67 }); 68 }1 private fun doquerybyname() { 2 val query = parsequery\<parseobject>("profile") 3 query wherecontains("name", "adam") 4 progressdialog!! show() 5 query findinbackground { objects list\<parseobject>?, e parseexception? > 6 progressdialog!! hide() 7 if (e == null) { 8 adapter = resultadapter(this, objects) 9 resultlist!! layoutmanager = linearlayoutmanager(this) 10 resultlist!! adapter = adapter 11 } else { 12 toast maketext(this, e localizedmessage, toast length short) show() 13 } 14 } 15 } 16 17 private fun doquerybyfriendcount() { 18 val query = parsequery\<parseobject>("profile") 19 query wheregreaterthan("friendcount", 20) 20 progressdialog!! show() 21 query findinbackground { objects list\<parseobject>?, e parseexception? > 22 progressdialog!! hide() 23 if (e == null) { 24 adapter = resultadapter(this, objects) 25 resultlist!! layoutmanager = linearlayoutmanager(this) 26 resultlist!! adapter = adapter 27 } else { 28 toast maketext(this, e localizedmessage, toast length short) show() 29 } 30 } 31 } 32 33 private fun doquerybyordering() { 34 val query = parsequery\<parseobject>("profile") 35 query orderbydescending("birthday") 36 progressdialog!! show() 37 query findinbackground { objects list\<parseobject>?, e parseexception? > 38 progressdialog!! hide() 39 if (e == null) { 40 adapter = resultadapter(this, objects) 41 resultlist!! layoutmanager = linearlayoutmanager(this) 42 resultlist!! adapter = adapter 43 } else { 44 toast maketext(this, e localizedmessage, toast length short) show() 45 } 46 } 47 } 48 49 private fun doquerybyall() { 50 val query = parsequery\<parseobject>("profile") 51 query wherecontains("name", "adam") 52 query wheregreaterthan("friendcount", 20) 53 query orderbydescending("birthday") 54 progressdialog!! show() 55 query findinbackground { objects list\<parseobject>?, e parseexception? > 56 progressdialog!! hide() 57 if (e == null) { 58 adapter = resultadapter(this, objects) 59 resultlist!! layoutmanager = linearlayoutmanager(this) 60 resultlist!! adapter = adapter 61 resultlist!! isnestedscrollingenabled = false 62 } else { 63 toast maketext(this, e localizedmessage, toast length short) show() 64 } 65 } 66 } เสร็จแล้ว! ในตอนท้ายของคู่มือนี้ คุณได้เรียนรู้ว่าการค้นหาข้อมูลพื้นฐานทำงานอย่างไรบน parse และวิธีการดำเนินการเหล่านี้บน back4app จากแอป android