Android
Data objects
Basic Queries
12 min
basic queries on android introduction in this guide, you will perform basic queries in parse parse and implement an android android app using these queries you will learn how to set up and query realistic data using back4app back4app and android android this tutorial uses an 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 android project built with this tutorial at our github repositories https //github com/templates back4app/android parse sdk kotlin https //github com/templates back4app/android parse sdk java goal our goal is query data stored on back4app back4app from an android android app here is a preview of what we are gonna achieve prerequisites to complete this tutorial, we need https //developer android com/studio/index html an app created on back4app note follow the https //www back4app com/docs/get started/new parse app to learn how to create a parse app on back4app an android app connected to back4app note follow the https //www back4app com/docs/android/parse android sdk l to create an android studio project connected to back4app a device (or https //developer android com/studio/run/managing avds html ) running android 4 1 (jelly bean) or newer let’s get started! before next steps, we need to connect back4app back4app to our application you should save the appid appid and clientkey clientkey from the back4app back4app to string xml string xml file and then init parse parse in our app java app java or app kt app kt file follow the https //www back4app com/docs/android/parse android sdk if you don’t know how to init parse parse to your app or you can download the projects we shared the github links above and edit only the appid appid and clientkey clientkey parts according to you 1 understanding the parse query class any parse query operation uses the parsequery parsequery object type, which will help you retrieve specific data from your database throughout your app it is crucial to know that a parsequery parsequery will only resolve after calling a retrieve method (like query findinbackground query findinbackground ), so a query can be set up and several modifiers can be chained before actually being called to create a new parsequery parsequery , you need to pass as a parameter the desired parseobject subclass, which is the one that will contain your query results an example query can be seen below, in which a fictional profile subclass is being queried 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() you can read more about the parse query parse query class https //www back4app com/docs/javascript/parse javascript sdk 2 save some data on back4app in this step, we will create a class with the js console and javascript codes provided by parse parse and we will create queries for this class let’s create a profile profile class, which will be the target of our queries in this guide on parse parse dashboard javascript console is possible to run javascript code directly, querying and updating your application database contents using the js sdk commands run the code below from your js console and insert the data on back4app here is how the js console looks like in your dashboard go ahead and create the user profile profile class with the following example content 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 query the data now that you have a populated class, we can now perform some basic queries in it let’s begin by filtering profile profile results by name, which is a string type field, searching for values that contain the name adam adam using the parse query contains parse query contains method 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 }; let’s now query by the number type field friendcount friendcount by using another common query method, parse query greaterthan parse query greaterthan in this case, we want user profiles profiles in which the friend count is greater than 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 }; other recurring query methods are parse query ascending parse query ascending and parse query descending parse query descending , responsible for ordering your queries this ordering can be done in most data types, so let’s order a query by the date field birthday birthday by the youngest 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 }; as stated here before, you can and should chain query methods to achieve more refined results let’s then combine the previous examples in a single query request 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 query from our android app we will now do the operations we did above from the js console with java and kotlin in our android application we will list the profiles by making 4 different queries 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 } it’s done! at the end of this guide, you learned how basic data queries work on parse and how to perform them on back4app from an android app