Basic Queries on Android
Introduction
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 Project via our GitHub repositories.
Goal
Our goal is query data stored on Back4App
from an Android
app.
Here is a preview of what we are gonna achieve:

Prerequisites
To complete this tutorial, we need:
- Android Studio.
- An app created on Back4App.
- Note: Follow the New Parse App tutorial to learn how to create a Parse App on Back4App.
- An android app connected to Back4App.
- 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 theappId
andclientKey
from theBack4App
tostring.xml
file and then initParse
in ourApp.java
orApp.kt
file.
Follow the New Parse App tutorial if you don’t know how to initParse
to your app.
Or you can download the projects we shared the github links above and edit only theappId
andclientKey
parts according to you.
Step 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.
1
2
3
4
// This will create your query
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
// The query will resolve only after calling this method
query.findInBackground();
1
2
3
4
// This will create your query
val query = ParseQuery<ParseObject>("Profile")
// The query will resolve only after calling this method
query.findInBackground()
You can read more about the Parse.Query
class here at the official documentation.
Step 2 - Save some data on Back4App
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 user Profile
class with the following example content:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Add Profile objects and create table
// Adam Sandler
let Profile = new Parse.Object('Profile');
Profile.set('name', 'Adam Sandler');
Profile.set('birthDay', new Date('09/09/1966'));
Profile.set('friendCount', 2);
Profile.set('favoriteFoods', ['Lobster', 'Bread']);
await Profile.save();
// Adam Levine
Profile = new Parse.Object('Profile');
Profile.set('name', 'Adam Levine');
Profile.set('birthDay', new Date('03/18/1979'));
Profile.set('friendCount', 52);
Profile.set('favoriteFoods', ['Cake', 'Bread']);
await Profile.save();
// Carson Kressley
Profile = new Parse.Object('Profile');
Profile.set('name', 'Carson Kressley');
Profile.set('birthDay', new Date('11/11/1969'));
Profile.set('friendCount', 12);
Profile.set('favoriteFoods', ['Fish', 'Cookies']);
await Profile.save();
// Dan Aykroyd
Profile = new Parse.Object('Profile');
Profile.set('name', 'Dan Aykroyd');
Profile.set('birthDay', new Date('07/01/1952'));
Profile.set('friendCount', 66);
Profile.set('favoriteFoods', ['Jam', 'Peanut Butter']);
await Profile.save();
// Eddie Murphy
Profile = new Parse.Object('Profile');
Profile.set('name', 'Eddie Murphy');
Profile.set('birthDay', new Date('04/03/1961'));
Profile.set('friendCount', 49);
Profile.set('favoriteFoods', ['Lettuce', 'Pepper']);
await Profile.save();
// Fergie
Profile = new Parse.Object('Profile');
Profile.set('name', 'Fergie');
Profile.set('birthDay', new Date('03/27/1975'));
Profile.set('friendCount', 55);
Profile.set('favoriteFoods', ['Lobster', 'Shrimp']);
await Profile.save();
console.log('Success!');
Step 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
results by name, which is a string type field, searching for values that contain the name Adam
using the Parse.Query.contains
method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Create your query
let parseQuery = new Parse.Query('Profile');
// `contains` is a basic query method that checks if string field
// contains a specific substring
parseQuery.contains('name', 'Adam');
// The query will resolve only after calling this method, retrieving
// an array of `Parse.Objects`
let queryResults = await parseQuery.find();
// Let's show the results
for (let result of queryResults) {
// You access `Parse.Objects` attributes by using `.get`
console.log(result.get('name'));
};
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Create your query
let parseQuery = new Parse.Query('Profile');
// `greaterThan` is a basic query method that does what it
// says on the tin
parseQuery.greaterThan('friendCount', 20);
// The query will resolve only after calling this method, retrieving
// an array of `Parse.Objects`
let queryResults = await parseQuery.find();
// Let's show the results
for (let result of queryResults) {
// You access `Parse.Objects` attributes by using `.get`
console.log(`name: ${result.get('name')}, friend count: ${result.get('friendCount')}`);
};
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Create your query
let parseQuery = new Parse.Query('Profile');
// `descending` and `ascending` can and should be chained
// with other query methods to improve your queries
parseQuery.descending('birthDay');
// The query will resolve only after calling this method, retrieving
// an array of `Parse.Objects`
let queryResults = await parseQuery.find();
// Let's show the results
for (let result of queryResults) {
// You access `Parse.Objects` attributes by using `.get`
console.log(`name: ${result.get('name')}, birthday: ${result.get('birthDay')}`);
};
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Create your query
let parseQuery = new Parse.Query('Profile');
parseQuery.contains('name', 'Adam');
parseQuery.greaterThan('friendCount', 20);
parseQuery.descending('birthDay');
// The query will resolve only after calling this method, retrieving
// an array of `Parse.Objects`
let queryResults = await parseQuery.find();
// Let's show the results
for (let result of queryResults) {
// You access `Parse.Objects` attributes by using `.get`
console.log(`name: ${result.get('name')}, friend count: ${result.get('friendCount')}, birthday: ${result.get('birthDay')}`);
};
Step 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
private void doQueryByName() {
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereContains("name", "Adam");
progressDialog.show();
query.findInBackground((objects, e) -> {
progressDialog.hide();
if (e == null) {
adapter = new ResultAdapter(this, objects);
resultList.setLayoutManager(new LinearLayoutManager(this));
resultList.setAdapter(adapter);
} else {
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void doQueryByFriendCount() {
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereGreaterThan("friendCount", 20);
progressDialog.show();
query.findInBackground((objects, e) -> {
progressDialog.hide();
if (e == null) {
adapter = new ResultAdapter(this, objects);
resultList.setLayoutManager(new LinearLayoutManager(this));
resultList.setAdapter(adapter);
} else {
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void doQueryByOrdering() {
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.orderByDescending("birthDay");
progressDialog.show();
query.findInBackground((objects, e) -> {
progressDialog.hide();
if (e == null) {
adapter = new ResultAdapter(this, objects);
resultList.setLayoutManager(new LinearLayoutManager(this));
resultList.setAdapter(adapter);
} else {
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private void doQueryByAll() {
ParseQuery<ParseObject> query = new ParseQuery<>("Profile");
query.whereContains("name", "Adam");
query.whereGreaterThan("friendCount", 20);
query.orderByDescending("birthDay");
progressDialog.show();
query.findInBackground((objects, e) -> {
progressDialog.hide();
if (e == null) {
adapter = new ResultAdapter(this, objects);
resultList.setLayoutManager(new LinearLayoutManager(this));
resultList.setAdapter(adapter);
resultList.setNestedScrollingEnabled(false);
} else {
Toast.makeText(this, e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
private fun doQueryByName() {
val query = ParseQuery<ParseObject>("Profile")
query.whereContains("name", "Adam")
progressDialog!!.show()
query.findInBackground { objects: List<ParseObject>?, e: ParseException? ->
progressDialog!!.hide()
if (e == null) {
adapter = ResultAdapter(this, objects)
resultList!!.layoutManager = LinearLayoutManager(this)
resultList!!.adapter = adapter
} else {
Toast.makeText(this, e.localizedMessage, Toast.LENGTH_SHORT).show()
}
}
}
private fun doQueryByFriendCount() {
val query = ParseQuery<ParseObject>("Profile")
query.whereGreaterThan("friendCount", 20)
progressDialog!!.show()
query.findInBackground { objects: List<ParseObject>?, e: ParseException? ->
progressDialog!!.hide()
if (e == null) {
adapter = ResultAdapter(this, objects)
resultList!!.layoutManager = LinearLayoutManager(this)
resultList!!.adapter = adapter
} else {
Toast.makeText(this, e.localizedMessage, Toast.LENGTH_SHORT).show()
}
}
}
private fun doQueryByOrdering() {
val query = ParseQuery<ParseObject>("Profile")
query.orderByDescending("birthDay")
progressDialog!!.show()
query.findInBackground { objects: List<ParseObject>?, e: ParseException? ->
progressDialog!!.hide()
if (e == null) {
adapter = ResultAdapter(this, objects)
resultList!!.layoutManager = LinearLayoutManager(this)
resultList!!.adapter = adapter
} else {
Toast.makeText(this, e.localizedMessage, Toast.LENGTH_SHORT).show()
}
}
}
private fun doQueryByAll() {
val query = ParseQuery<ParseObject>("Profile")
query.whereContains("name", "Adam")
query.whereGreaterThan("friendCount", 20)
query.orderByDescending("birthDay")
progressDialog!!.show()
query.findInBackground { objects: List<ParseObject>?, e: ParseException? ->
progressDialog!!.hide()
if (e == null) {
adapter = ResultAdapter(this, objects)
resultList!!.layoutManager = LinearLayoutManager(this)
resultList!!.adapter = adapter
resultList!!.isNestedScrollingEnabled = false
} else {
Toast.makeText(this, e.localizedMessage, Toast.LENGTH_SHORT).show()
}
}
}
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.