How to Update Data in Back4App Database in Android?
Last Updated :
24 May, 2021
In the previous article, we have seen adding as well as reading data from our Bac4App database in the Android app. In this article, we will take a look at Updating this data in your database.
What we are going to build in this article?
We will be building a simple application in which we will be updating the previously added data in our Back4App database.
Step by Step Implementation
Step 1: Creating a new Activity for updating the data
For creating a new Activity navigate to the app > res > layout > Right-Click on it and click on New > then click on Empty Activity to create a new Activity, and we will name it as UpdateCourseActivity.
Step 2: Updating our Adapter class for opening a new activity on clicking the RecyclerView item
As we have created our Adapter class in the previous article for displaying the list of courses in Recycler View. In this article, we will add onClickListener() inside that Adapter class for RecyclerView item click listener. Add the following code snippet to the CourseRVAdapter.java file.
Java
// adding on click listener for our item of recycler view.
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calling a intent to open new activity.
Intent i = new Intent(context, UpdateCourseActivity.class);
// on below line we are passing data to our intent on below line.
i.putExtra("courseName", courses.getCourseName());
i.putExtra("courseDescription", courses.getCourseDescription());
i.putExtra("courseDuration", courses.getCourseDuration());
// starting our activity on below line.
context.startActivity(i);
}
});
}
Below is the updated code for the CourseRVAdapter.java file.
Java
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class CourseRVAdapter extends RecyclerView.Adapter<CourseRVAdapter.ViewHolder> {
private Context context;
private ArrayList<CourseModal> courseModalArrayList;
// creating a constructor class.
public CourseRVAdapter(Context context, ArrayList<CourseModal> courseModalArrayList) {
this.context = context;
this.courseModalArrayList = courseModalArrayList;
}
@NonNull
@Override
public CourseRVAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
// passing our layout file for displaying our card item
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.course_rv_item, parent, false));
}
@Override
public void onBindViewHolder(@NonNull CourseRVAdapter.ViewHolder holder, int position) {
// setting data to our text views from our modal class.
CourseModal courses = courseModalArrayList.get(position);
holder.courseNameTV.setText(courses.getCourseName());
holder.courseDurationTV.setText(courses.getCourseDuration());
holder.courseDescTV.setText(courses.getCourseDescription());
// adding on click listener for our item of recycler view.
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calling a intent to open new activity.
Intent i = new Intent(context, UpdateCourseActivity.class);
// on below line we are passing data to our intent on below line.
i.putExtra("courseName", courses.getCourseName());
i.putExtra("courseDescription", courses.getCourseDescription());
i.putExtra("courseDuration", courses.getCourseDuration());
// starting our activity on below line.
context.startActivity(i);
}
});
}
@Override
public int getItemCount() {
return courseModalArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
// creating variables for our text views.
private final TextView courseNameTV;
private final TextView courseDurationTV;
private final TextView courseDescTV;
public ViewHolder(@NonNull View itemView) {
super(itemView);
// initializing our text views.
courseNameTV = itemView.findViewById(R.id.idTVCourseName);
courseDurationTV = itemView.findViewById(R.id.idTVCourseDuration);
courseDescTV = itemView.findViewById(R.id.idTVCourseDescription);
}
}
}
Step 3: Working with the activity_update_course.xml file
Navigate to the app > res > layout > activity_update_course.xml and add the below code to it.
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".UpdateCourseActivity">
<!--Edit text for getting course Name-->
<EditText
android:id="@+id/idEdtCourseName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:hint="Course Name"
android:importantForAutofill="no"
android:inputType="text" />
<!--Edittext for getting course Duration-->
<EditText
android:id="@+id/idEdtCourseDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:hint="Course Duration in min"
android:importantForAutofill="no"
android:inputType="time" />
<!--Edittext for getting course Description-->
<EditText
android:id="@+id/idEdtCourseDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:hint="Course Description"
android:importantForAutofill="no"
android:inputType="text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="10dp"
android:orientation="horizontal"
android:weightSum="2">
<!--button for updating our course-->
<Button
android:id="@+id/idBtnUpdate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Update Course"
android:textAllCaps="false" />
</LinearLayout>
</LinearLayout>
Step 4: Working with the UpdateCourseActivity.java file
Navigate to the app > java > your app's package name > UpdateCourseActivity.java file and add the below code to it. Comments are added inside the code to understand the code in more detail.
Java
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.SaveCallback;
public class UpdateCourseActivity extends AppCompatActivity {
// creating variables for our edit text
private EditText courseNameEdt, courseDurationEdt, courseDescriptionEdt;
// creating variable for button
private Button updateCourseBtn;
// creating a strings for storing our values from edittext fields.
private String courseName, courseDuration, courseDescription, originalCourseName, objectID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_course);
// initializing our edittext and buttons
updateCourseBtn = findViewById(R.id.idBtnUpdate);
courseNameEdt = findViewById(R.id.idEdtCourseName);
courseDescriptionEdt = findViewById(R.id.idEdtCourseDescription);
courseDurationEdt = findViewById(R.id.idEdtCourseDuration);
// on below line we are setting data to our edit text field.
courseNameEdt.setText(getIntent().getStringExtra("courseName"));
courseDescriptionEdt.setText(getIntent().getStringExtra("courseDescription"));
courseDurationEdt.setText(getIntent().getStringExtra("courseDuration"));
originalCourseName = getIntent().getStringExtra("courseName");
updateCourseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
courseName = courseNameEdt.getText().toString();
courseDescription = courseDescriptionEdt.getText().toString();
courseDuration = courseDurationEdt.getText().toString();
// validating the text fields if empty or not.
if (TextUtils.isEmpty(courseName)) {
courseNameEdt.setError("Please enter Course Name");
} else if (TextUtils.isEmpty(courseDescription)) {
courseDescriptionEdt.setError("Please enter Course Description");
} else if (TextUtils.isEmpty(courseDuration)) {
courseDurationEdt.setError("Please enter Course Duration");
} else {
// calling method to update data.
updateData(originalCourseName, courseName, courseDescription, courseDuration);
}
}
});
}
private void updateData(String originalCourseName, String courseName, String courseDescription, String courseDuration) {
// Configure Query with our query.
ParseQuery<ParseObject> query = ParseQuery.getQuery("courses");
// adding a condition where our course name must be equal to the original course name
query.whereEqualTo("courseName", originalCourseName);
// in below method we are getting the unique id
// of the course which we have to make update.
query.getFirstInBackground(new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException e) {
// inside done method we check
// if the error is null or not.
if (e == null) {
// if the error is null then we are getting
// our object id in below line.
objectID = object.getObjectId().toString();
// after getting our object id we will
// move towards updating our course.
// calling below method to update our course.
query.getInBackground(objectID, new GetCallback<ParseObject>() {
@Override
public void done(ParseObject object, ParseException e) {
// in this method we are getting the
// object which we have to update.
if (e == null) {
// in below line we are adding new data
// to the object which we get from its id.
// on below line we are adding our data
// with their key value in our object.
object.put("courseName", courseName);
object.put("courseDescription", courseDescription);
object.put("courseDuration", courseDuration);
// after adding new data then we are
// calling a method to save this data
object.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
// inside on done method we are checking
// if the error is null or not.
if (e == null) {
// if the error is null our data has been updated.
// we are displaying a toast message and redirecting
// our user to home activity where we are displaying course list.
Toast.makeText(UpdateCourseActivity.this, "Course Updated..", Toast.LENGTH_SHORT).show();
Intent i = new Intent(UpdateCourseActivity.this, HomeActivity.class);
startActivity(i);
} else {
// below line is for error handling.
Toast.makeText(UpdateCourseActivity.this, "Fail to update data " + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
});
} else {
// on below line we are displaying a message
// if we don't get the object from its id.
Toast.makeText(UpdateCourseActivity.this, "Fail to update course " + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
});
} else {
// this is error handling if we don't get the id for our object
Toast.makeText(UpdateCourseActivity.this, "Fail to get object ID..", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Now run your app and try updating your course. Make sure you have previously added some courses to your list.
Output:
Below is the file structure in Android Studio after performing the Update operation:
Similar Reads
How to Add Data to Back4App Database in Android?
Prerequisite: How to Connect Android App with Back4App? Back4App is an online database providing platform that provides us services with which we can manage the data of our app inside the database. This is a series of 4 articles in which we are going to perform the basic CRUD (Create, Read, Update,
4 min read
How to Delete Data in Back4App Database in Android?
We have seen adding data to our Back4App database along with reading and updating this data. In this article, we will take a look at Deleting this data. What we are going to build? We will be adding a simple button inside our update screen where we were updating our data and inside that screen, we
7 min read
How to Update Data in Realm Database in Android?
In previous articles, we have seen adding and reading data from our realm database in Android. In that article, we were adding course details in our database and reading the data in the form of a list. In this article, we will take a look at updating this data in our android app. What we are going
7 min read
How to Update Data to SQLite Database in Android?
We have seen How to Create and Add Data to SQLite Database in Android as well as How to Read Data from SQLite Database in Android. We have performed different SQL queries for reading and writing our data to SQLite database. In this article, we will take a look at updating data to SQLite database in
10 min read
How to Read Data from Back4App Database in Android?
We have seen adding data to the Back4App database in the Android app. In this article, we will take a look at reading this data from our database in our Android App. What we are going to build in this article? We will be creating a new screen in the previous application and inside that, we will dis
9 min read
How to Update Data in Firebase Firestore in Android?
In the previous article, we have seen on How to Add Data to Firebase Firestore in Android, How to Read the data from Firebase Firestore in Android. Now we will see How to Update this added data inside our Firebase Firestore. Now we will move towards the implementation of this updating data in Androi
10 min read
How to Update Data to SQLite Database in Android using Jetpack Compose?
We have seen How to Create and Add Data to SQLite Database in Android using Jetpack Compose as well as How to Read Data from SQLite Database in Android using Jetpack Compose. We have performed different SQL queries for reading and writing our data to SQLite database. In this article, we will take a
11 min read
How to Create and Add Data to SQLite Database in Android?
SQLite is another data storage available in Android where we can store data in the user's device and can use it any time when required. In this article, we will take a look at creating an SQLite database in the Android app and adding data to that database in the Android app. This is a series of 4 ar
8 min read
How to Update Data in API using Volley in Android?
Prerequisite: JSON Parsing in Android using Volley LibraryHow to Post Data to API using Volley in Android? We have seen reading data from API as well as posting data to our database with the help of the API. In this article, we will take a look at updating our data in our API. We will be using the V
5 min read
How to Delete Data in SQLite Database in Android?
In the previous articles, we have seen three operations of CRUD operations such as create, read and update operations in our Android app. In this article, we will take a look at adding delete operation for deleting our items stored in the SQLite database. What we are going to build in this article?
8 min read