Parse Data Types on Android
Introduction
In this guide, you will learn about the Parse Datatypes using Android. You will read and save the Parse Objects on Back4App from an Android App.
Storing data on Parse is built around the ParseObject
. Each ParseObject
contains key-value pairs of JSON-compatible data. This data is schemaless, which means that we don’t need to specify ahead of time what keys exist on each ParseObject
. We can set whatever key-value pairs we want, and our backend will store them. For example, let’s say we’re tracking high scores for a game. A single ParseObject
could contain:
1
score: 1337, playerName: "Sean Plott", cheatMode: false
Keys must be alphanumeric strings, and values can be:
String
=>String
Number
(primitive numeric values such asint
,double
)Bool
=>boolean
DateTime
=> java.util.DateNull
=> JSONObject.NULLArray
=> JSONArrayFile
=> ParseFilePointer
=> other ParseObjectRelation
=> ParseRelationGeopoint
=> ParseGeoPoint
Each ParseObject
has a class name that we can use to distinguish different sorts of data. For example, we could call the high score object a GameScore
. There are also a few fields we don’t need to specify that are provided as a convenience:
objectId
is a unique identifier for each saved object.createdAt
andupdatedAt
represent the time that each object was created and last modified in the cloud.
Each of these fields is automatically filled in by Back4app at the moment we save a new ParseObject
.
We recommend you
NameYourClassesLikeThis
(UpperCamelCase) andnameYourKeysLikeThis
(lowerCamelCase), just to keep your code looking pretty.
This tutorial uses a basic 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 to create an Android App that can process all data types provided by Parse Server
.
Here is a preview of what we are gonna achive :
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.
Understanding our App
You will create an app for a better understanding of Parse Data Types
. In this Android
app, you will create all data types in a class named DataTypes
and assign values to this classes variables. Then we will read and update this data.
- Note The data types
Pointer
,Relation
,File
,Geopoint
will be covered later in specific guides.
Let’s get started!
Step 1 - Create App Template
Define the following variables in the MainActivity
and replace the code in the onCreate
method with the following code.
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
private ProgressDialog progressDialog;
private View popupInputDialogView;
private RecyclerView recyclerView;
private String objectId;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDialog = new ProgressDialog(MainActivity.this);
Button saveData = findViewById(R.id.saveData);
Button readData = findViewById(R.id.readData);
Button updateData = findViewById(R.id.updateData);
saveData.setOnClickListener(saveDataView -> {
try {
saveDataTypes();
} catch (JSONException e) {
e.printStackTrace();
}
});
readData.setOnClickListener(readDataView -> readObjects());
updateData.setOnClickListener(updateDataView -> updateObject());
}
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
private var progressDialog: ProgressDialog? = null
private var objectId: String? = null
private var popupInputDialogView: View? = null
private var recyclerView: RecyclerView? = null
private val TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
progressDialog = ProgressDialog(this@MainActivity)
val saveData = findViewById<Button>(R.id.saveData)
val readData = findViewById<Button>(R.id.readData)
val updateData = findViewById<Button>(R.id.updateData)
saveData.setOnClickListener {
try {
saveDataTypes()
} catch (e: JSONException) {
e.printStackTrace()
}
}
readData.setOnClickListener { readObjects() }
updateData.setOnClickListener { updateObject() }
}
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.
Step 2 - Code for Save Object
The create function will create a new object in Back4app database. We define the saveDataTypes
function that we call in the onCreate
function and use the following code in this function. We will save all data types with this function to the DataTypes
classes object.
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
private void saveDataTypes() throws JSONException{
ParseObject parseObject = new ParseObject("DataTypes");
parseObject.put("stringField", "String");
parseObject.put("doubleField", 1.5);
parseObject.put("intField", 2);
parseObject.put("boolField", true);
parseObject.put("dateField", Calendar.getInstance().getTime());
JSONObject myObject = new JSONObject();
myObject.put("number", 1);
myObject.put("string", "42");
parseObject.put("jsonObject", myObject);
JSONArray myArray = new JSONArray();
myArray.put(myObject);
myArray.put(myObject);
myArray.put(myObject);
parseObject.put("jsonArray", myArray);
List<String> list = new ArrayList<>();
list.add("string1");
list.add("string2");
parseObject.put("listStringField", list);
List<Integer> listInt = new ArrayList<>();
listInt.add(1);
listInt.add(2);
listInt.add(3);
parseObject.put("listIntField", listInt);
List<Boolean> listBool = new ArrayList<>();
listBool.add(true);
listBool.add(false);
parseObject.put("listBoolField", listBool);
progressDialog.show();
parseObject.saveInBackground(e -> {
progressDialog.dismiss();
if (e == null) {
Toast.makeText(this, "Object created successfully...", Toast.LENGTH_SHORT).show();
objectId = parseObject.getObjectId();
} else {
objectId = null;
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).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
private fun saveDataTypes() {
val parseObject = ParseObject("DataTypes")
parseObject.put("stringField", "String")
parseObject.put("doubleField", 1.5)
parseObject.put("intField", 2)
parseObject.put("boolField", true)
parseObject.put("dateField", Calendar.getInstance().time)
val myObject = JSONObject()
myObject.put("number", 1)
myObject.put("string", "42")
parseObject.put("jsonObject", myObject)
val myArray = JSONArray()
myArray.put(myObject)
myArray.put(myObject)
myArray.put(myObject)
parseObject.put("jsonArray", myArray)
val list: MutableList<String> = ArrayList()
list.add("string1")
list.add("string2")
parseObject.put("listStringField", list)
val listInt: MutableList<Int> = ArrayList()
listInt.add(1)
listInt.add(2)
listInt.add(3)
parseObject.put("listIntField", listInt)
val listBool: MutableList<Boolean> = ArrayList()
listBool.add(true)
listBool.add(false)
parseObject.put("listBoolField", listBool)
progressDialog?.show()
parseObject.saveInBackground {
progressDialog?.dismiss()
if (it == null) {
Toast.makeText(this, "Object created successfully...", Toast.LENGTH_SHORT).show()
objectId = parseObject.objectId
} else {
objectId = null
Toast.makeText(this, it.message, Toast.LENGTH_LONG).show()
}
}
}
Step 3 - Code for Read Object
We will give the objectId
variable that we have assigned in the saveDataTypes
function as a parameter to the query and read the data that we have saved in the saveDataTypes
function with the readObjects
function.
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
private void readObjects() {
if (objectId == null) {
Toast.makeText(this, "No object. Click on the 'Save Data' button before.", Toast.LENGTH_SHORT).show();
return;
}
ParseQuery<ParseObject> query = new ParseQuery<>("DataTypes");
progressDialog.show();
query.getInBackground(objectId, (object, e) -> {
progressDialog.dismiss();
if (e == null) {
List<Data> list = new ArrayList<>();
list.add(new Data("Int list field",object.get("listIntField").toString()));
list.add(new Data("String field",object.get("stringField").toString()));
list.add(new Data("Double field",object.get("doubleField").toString()));
list.add(new Data("Int field",object.get("intField").toString()));
list.add(new Data("String list field",object.get("listStringField").toString()));
list.add(new Data("Date field",object.get("dateField").toString()));
list.add(new Data("Bool field",object.get("boolField").toString()));
list.add(new Data("List Bool field",object.get("listBoolField").toString()));
list.add(new Data("Json Object field",object.get("jsonObject").toString()));
list.add(new Data("Json Array field",object.get("jsonArray").toString()));
showDataTypes(list);
} else {
Toast.makeText(this, e.getMessage(), 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
private fun readObjects() {
if (objectId == null) {
Toast.makeText(
this,
"No object. Click on the 'Save Data' button before.",
Toast.LENGTH_SHORT
).show()
return
}
val query = ParseQuery<ParseObject>("DataTypes")
progressDialog?.show()
query.getInBackground(
objectId
) { obj, e ->
progressDialog?.dismiss()
if (e == null) {
val list: MutableList<Data> = ArrayList()
list.add(Data("Int list field", obj.get("listIntField").toString()))
list.add(Data("String field",obj.get("stringField").toString()))
list.add(Data("Double field", obj.get("doubleField").toString()))
list.add(Data("Int field", obj.get("intField").toString()))
list.add(Data("String list field", obj.get("listStringField").toString()))
list.add(Data("Date field",obj.get("dateField").toString()))
list.add(Data("Bool field", obj.get("boolField").toString()))
list.add(Data("List Bool field", obj.get("listBoolField").toString()))
list.add(Data("Json Object field", obj.get("jsonObject").toString()))
list.add(Data("Json Array field", obj.get("jsonArray").toString()))
showDataTypes(list)
} else {
Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show()
}
}
}
In this section, we have created a model class called Data
. We use the data we get in the readObjects
function to create objects from this model class. We give these objects as elements to the list we created in Data
type. Then we give this list as a parameter to the showDataTypes
function and list it in the AlertDialog
.
This is the Data
model.
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
public class Data {
private String type;
private String value;
public Data(String type, String value) {
this.type = type;
this.value = value;
}
public String getType() {
return type;
}
public Data setType(String type) {
this.type = type;
return this;
}
public String getValue() {
return value;
}
public Data setValue(String value) {
this.value = value;
return this;
}
}
1
2
3
class Data(val type:String?=null,val value:String?=null) {
}
This is the showDataTypes
function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void showDataTypes(List<Data> list){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setTitle("Data types");
alertDialogBuilder.setCancelable(true);
initPopupViewControls(list);
//We are setting our custom popup view by AlertDialog.Builder
alertDialogBuilder.setView(popupInputDialogView);
final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
private void initPopupViewControls(List<Data> list) {
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
popupInputDialogView = layoutInflater.inflate(R.layout.custom_alert_dialog, null);
recyclerView = popupInputDialogView.findViewById(R.id.recyclerView);
ItemAdapter adapter = new ItemAdapter(list,this);
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
recyclerView.setAdapter(adapter);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
private fun showDataTypes(list: List<Data>) {
val alertDialogBuilder = AlertDialog.Builder(this@MainActivity)
alertDialogBuilder.setTitle("Data types")
alertDialogBuilder.setCancelable(true)
initPopupViewControls(list)
//We are setting our custom popup view by AlertDialog.Builder
alertDialogBuilder.setView(popupInputDialogView)
val alertDialog = alertDialogBuilder.create()
alertDialog.show()
}
@SuppressLint("InflateParams")
private fun initPopupViewControls(list: List<Data>) {
val layoutInflater = LayoutInflater.from(this@MainActivity)
popupInputDialogView = layoutInflater.inflate(R.layout.custom_alert_dialog, null)
recyclerView = popupInputDialogView?.findViewById(R.id.recyclerView)
val adapter = ItemAdapter(this@MainActivity, list)
recyclerView?.layoutManager = LinearLayoutManager(
this,
LinearLayoutManager.VERTICAL,
false
)
recyclerView?.adapter = adapter
}
Step 4 - Code for Update Object
The updateObject
function is responsible for updating data in the object created on the saveDataTypes
function. We are using objectId
again for update object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void updateObject() {
if (objectId == null) {
Toast.makeText(this, "No object. Click on the 'Save Data' button before.", Toast.LENGTH_SHORT).show();
return;
}
ParseObject parseObject = new ParseObject("DataTypes");
parseObject.setObjectId(objectId);
parseObject.put("intField", 5);
parseObject.put("stringField", "new String");
progressDialog.show();
parseObject.saveInBackground(e -> {
progressDialog.dismiss();
if (e == null) {
Toast.makeText(this, "Object updated successfully...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, e.getMessage(), 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
private fun updateObject() {
if (objectId == null) {
Toast.makeText(
this,
"No object. Click on the 'Save Data' button before.",
Toast.LENGTH_SHORT
).show()
return
}
val parseObject = ParseObject("DataTypes")
parseObject.objectId = objectId
parseObject.put("intField", 5)
parseObject.put("stringField", "new String")
progressDialog?.show()
parseObject.saveInBackground {
progressDialog?.dismiss()
if (it == null) {
Toast.makeText(this, "Object updated successfully...", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, it.message, Toast.LENGTH_SHORT).show()
}
}
}
Step 5 - Using Counters
The above example contains a common use case. The intField
field can be a counter that we’ll need to update continually. The above solution works, but it’s cumbersome and can lead to problems if we have multiple clients trying to update the same counter. Parse provides methods that atomically increment any number field to help with storing counter-type data. So, the same update can be rewritten as:
1
2
3
ParseObject parseObject = new ParseObject("DataTypes");
parseObject.setObjectId(objectId);
parseObject.increment("intField",1);
1
2
3
val parseObject = ParseObject("DataTypes")
parseObject.objectId = objectId
parseObject.increment("intField",1)
Step 6 - Using Lists
Parse also provides methods to help in storing list data. There are three operations that can be used to change a list field atomically:
setAdd
andsetAddAll
: append the given objects to the end of an array field.setAddUnique
andsetAddAllUnique
: add only the given objects which aren’t already contained in an array field to that field. The position of the insert is not guaranteed.remove
andremoveAll
: removes all instances of the given objects from an array field.
Step 6.1 - Examples with add
and addAll
listStringField
has the value:
1
["a","b","c","d","e","f","g","g"]
Running the code below:
1
2
3
4
5
ParseObject parseObject = new ParseObject("DataTypes");
parseObject.setObjectId(objectId);
parseObject.add("listStringField","e");
parseObject.addAll("listStringField", Arrays.asList("e", "f", "g", "g"));
parseObject.save();
1
2
3
4
5
val parseObject = ParseObject("DataTypes")
parseObject.objectId = objectId
parseObject.add("listStringField", "e")
parseObject.addAll("listStringField", Arrays.asList("e", "f", "g", "g"))
parseObject.save()
After this command the result of the stringList
field will be:
1
["a","b","c","d","e","e","f","g","g"]
Step 6.2 - Examples with addUnique
and addAllUnique
listStringField
has the value:
1
["a","b","c","d","e"]
Running the code below:
1
2
3
4
5
ParseObject parseObject = new ParseObject("DataTypes");
parseObject.setObjectId(objectId);
parseObject.addUnique("listStringField","e");
parseObject.addAllUnique("listStringField",Arrays.asList("c", "d", "e", "f"));
parseObject.save();
1
2
3
4
5
val parseObject = ParseObject("DataTypes")
parseObject.objectId = objectId
parseObject.addUnique("listStringField", "e")
parseObject.addAllUnique("listStringField", Arrays.asList("c", "d", "e", "f"))
parseObject.save()
After this command the result of the stringList
field will be:
1
["a","b","c","d","e","f"]
No values were repeated.
Step 6.3 - Examples with removeAll
listStringField
has the value:
1
["a","b","c","d","e","f"]
Running the code below:
1
2
3
4
ParseObject parseObject = new ParseObject("DataTypes");
parseObject.setObjectId(objectId);
parseObject.removeAll("listStringField",Arrays.asList("c", "d", "e", "f"));
parseObject.save();
1
2
3
4
val parseObject = ParseObject("DataTypes")
parseObject.objectId = objectId
parseObject.removeAll("listStringField", Arrays.asList("c", "d", "e", "f"))
parseObject.save()
After this command the result of the stringList
field will be:
1
["a","b"]
Note that it is not currently possible to atomically add and remove items from an array in the same save. We will have to call the save for every different array operation we want to perform separately.
Step 7 - Remove single field from ParseObject
You can delete a single field from an object by using the remove
operation:
1
2
3
ParseObject parseObject = new ParseObject("DataTypes");
parseObject.remove("stringField");
parseObject.save();
1
2
3
val parseObject = ParseObject("DataTypes")
parseObject.remove("stringField")
parseObject.save()
It’s done!
At this point, we have learned Parse Data Types
on Android
.