In this guide, you will perform basic queries in Parse and implement an Android app using these queries. You will learn how to set up and query realistic data using Back4App and Android.
This tutorial uses an app created in Android Studio 4.1.1 with buildToolsVersion=30.0.2 , Compile SDK Version = 30.0.2 and targetSdkVersion=30
At any time, you can access the complete Android Project built with this tutorial at our Github repositories
Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
A device (or virtual device) running Android 4.1 (Jelly Bean) or newer.
Let’s get started!
Before next steps, we need to connect Back4App to our application. You should save the appId and clientKey from the Back4App to string.xml file and then init Parse in our App.java or App.kt file.
Follow the New Parse App tutorial if you don’t know how to init Parse to your app.
Or you can download the projects we shared the github links above and edit only the appId and clientKey parts according to you.
1 - Understanding the Parse.Query class
Any Parse query operation uses the ParseQuery object type, which will help you retrieve specific data from your database throughout your app. It is crucial to know that a ParseQuery will only resolve after calling a retrieve method (like query.findInBackground), so a query can be set up and several modifiers can be chained before actually being called.
To create a new 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.
Java
Kotlin
1// This will create your query2 val query = ParseQuery<ParseObject>("Profile")3// The query will resolve only after calling this method4 query.findInBackground()
In this step, we will create a Class with the JS Console and Javascript codes provided by Parse and we will create queries for this Class.
Let’s create a Profile class, which will be the target of our queries in this guide. On 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 userProfileclass with the following example content:
Now that you have a populated class, we can now perform some basic queries in it. Let’s begin by filtering Profile results by name, which is a string type field, searching for values that contain the name Adam using the Parse.Query.contains method:
Java
1// Create your query2 let parseQuery =newParse.Query('Profile');34// `contains` is a basic query method that checks if string field5// contains a specific substring6 parseQuery.contains('name','Adam');78// The query will resolve only after calling this method, retrieving9// an array of `Parse.Objects`10 let queryResults = await parseQuery.find();1112// Let's show the results13for(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 by using another common query method, Parse.Query.greaterThan. In this case, we want user Profiles in which the friend count is greater than 20.
Java
1// Create your query2 let parseQuery =newParse.Query('Profile');34// `greaterThan` is a basic query method that does what it5// says on the tin6 parseQuery.greaterThan('friendCount',20);78// The query will resolve only after calling this method, retrieving9// an array of `Parse.Objects`10 let queryResults = await parseQuery.find();1112// Let's show the results13for(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 and 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 by the youngest.
Java
1// Create your query2 let parseQuery =newParse.Query('Profile');34// `descending` and `ascending` can and should be chained5// with other query methods to improve your queries6 parseQuery.descending('birthDay');78// The query will resolve only after calling this method, retrieving9// an array of `Parse.Objects`10 let queryResults = await parseQuery.find();1112// Let's show the results13for(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:
Java
1// Create your query2 let parseQuery =newParse.Query('Profile');34 parseQuery.contains('name','Adam');5 parseQuery.greaterThan('friendCount',20);6 parseQuery.descending('birthDay');78// The query will resolve only after calling this method, retrieving9// an array of `Parse.Objects`10 let queryResults = await parseQuery.find();1112// Let's show the results13for(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.