Android
Data objects
在Android应用上使用Parse执行基础数据查询指南
11 分
android上的基本查询 介绍 在本指南中,您将执行基本查询,并使用这些查询实现一个 parse parse 和一个 android android 应用。您将学习如何使用 back4app back4app 和 android android 设置和查询真实数据。 本教程使用在 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 仓库中构建的完整 android 项目 https //github com/templates back4app/android parse sdk kotlin https //github com/templates back4app/android parse sdk java 目标 我们的目标是从一个 back4app back4app 应用中查询存储的数据 android android 这是我们将要实现的预览 先决条件 要完成本教程,我们需要: 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(果冻豆)或更高版本的设备(或 https //developer android com/studio/run/managing avds html )。 让我们开始吧! 在下一步之前,我们需要将 back4app back4app 连接到我们的应用程序。您应该保存 appid appid 和 clientkey clientkey 从 back4app back4app 到 string xml string xml 文件,然后在我们的 app java app java 或 app kt app kt 文件中初始化 parse parse 。 如果您不知道如何将 parse parse 初始化到您的应用程序中,请遵循 https //www back4app com/docs/android/parse android sdk 。 或者您可以下载我们在上面共享的 github 链接的项目,并仅根据您的需要编辑 appid appid 和 clientkey clientkey 部分。 1 理解 parse query 类 任何 parse 查询操作都使用 parsequery parsequery 对象类型,这将帮助您在整个应用程序中从数据库中检索特定数据。重要的是要知道,只有在调用检索方法(如 query findinbackground query findinbackground )后, parsequery parsequery 才会解析,因此可以设置查询并在实际调用之前链接多个修饰符。 要创建一个新的 parsequery parsequery ,您需要将所需的 parseobject 子类作为参数传递,该子类将包含您的查询结果。下面可以看到一个示例查询,其中查询了一个虚构的 profile 子类。 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() 您可以在官方文档中阅读更多关于 parse query parse query 类的信息, https //www back4app com/docs/javascript/parse javascript sdk 。 2 在 back4app 上保存一些数据 在此步骤中,我们将使用 parse parse 提供的 js 控制台和 javascript 代码创建一个类,并为该类创建查询。 让我们创建一个 个人资料 个人资料 类,这将是我们在本指南中查询的目标。在 parse parse 仪表板的 javascript 控制台中,可以直接运行 javascript 代码,使用 js sdk 命令查询和更新您的应用程序数据库内容。从您的 js 控制台运行下面的代码,并将数据插入到 back4app 中。 以下是您仪表板中 js 控制台的样子: 继续创建用户 个人资料 个人资料 类,内容如下示例: 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 查询数据 现在您已经有了一个填充的类,我们可以在其中执行一些基本查询。让我们开始通过名称过滤 个人资料 个人资料 结果,这是一个字符串类型字段,搜索包含名称 亚当 亚当 的值,使用 parse query contains parse query contains 方法 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 }; 现在让我们通过数字类型字段 friendcount friendcount 使用另一个常见的查询方法, parse query greaterthan parse query greaterthan 在这种情况下,我们想要用户 profiles profiles 其中好友数量大于 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 }; 其他常见的查询方法是 parse query ascending parse query ascending 和 parse query descending parse query descending , 负责对你的查询进行排序。这个排序可以在大多数数据类型中进行,所以让我们按最年轻的日期字段 birthday birthday 排序。 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应用程序查询 我们现在将在我们的android应用程序中使用java和kotlin执行上述在js控制台中进行的操作。我们将通过进行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上进行基本数据查询,以及如何从android应用程序在back4app上执行这些查询。