Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
How to Read Data from Back4App Database in Android?
Next article icon

How to Delete Data in Back4App Database in Android?

Last Updated : 24 May, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

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 will be adding a new button for deleting the course. After deleting, that course will be deleted from our database. 

Step by Step Implementation

Step 1: Creating a new button for deleting the data inside the activity_update_course.xml file

As we have created a new Update Course Activity in the previous article. So we will simply add a new button to it. Add the following code snippet to the activity_update_course.xml file. 

XML
<!--button for deleting our course--> <Button   android:id="@+id/idBtnDelete"   android:layout_width="0dp"   android:layout_height="wrap_content"   android:layout_margin="3dp"   android:layout_weight="1"   android:text="Delete Course"   android:textAllCaps="false" /> 

Below is the updated code for the activity_update_course.xml file. 

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">          <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_margin="3dp"                 android:layout_weight="1"                 android:text="Update Course"                 android:textAllCaps="false" />              <!--button for deleting our course-->             <Button                 android:id="@+id/idBtnDelete"                 android:layout_width="0dp"                 android:layout_height="wrap_content"                 android:layout_margin="3dp"                 android:layout_weight="1"                 android:text="Delete Course"                 android:textAllCaps="false" />          </LinearLayout>      </LinearLayout>  </LinearLayout> 

Step 2: Now we have to initialize this button in the UpdateCourseActivity.java file and add onClickListener to it 

Go to the UpdateCourseActivity.java file and add the following code snippet.

Java
// Adding on click listener for delete button deleteCourseBtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 // calling a method to delete a course.                 deleteCourse(originalCourseName);         } });  private void deleteCourse(String courseName) {          // 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", courseName);          // on below line we are finding the course with the course name.         query.findInBackground(new FindCallback<ParseObject>() {             @Override             public void done(List<ParseObject> objects, ParseException e) {                 // if the error is null.                 if (e == null) {                     // on below line we are getting the first course and                     // calling a delete method to delete this course.                     objects.get(0).deleteInBackground(new DeleteCallback() {                         @Override                         public void done(ParseException e) {                             // inside done method checking if the error is null or not.                             if (e == null) {                                 // if the error is not null then we are displaying a toast message and opening our home activity.                                 Toast.makeText(UpdateCourseActivity.this, "Course Deleted..", Toast.LENGTH_SHORT).show();                                 Intent i = new Intent(UpdateCourseActivity.this, HomeActivity.class);                                 startActivity(i);                             } else {                                 // if we get error we are displaying it in below line.                                 Toast.makeText(UpdateCourseActivity.this, "Fail to delete course..", Toast.LENGTH_SHORT).show();                             }                         }                     });                 } else {                     // if we don't get the data in our database then we are displaying below message.                     Toast.makeText(UpdateCourseActivity.this, "Fail to get the object..", Toast.LENGTH_SHORT).show();                 }             }      }); } 

Below is the updated code for the UpdateCourseActivity.java file. 

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.DeleteCallback; import com.parse.FindCallback; import com.parse.GetCallback; import com.parse.ParseException; import com.parse.ParseObject; import com.parse.ParseQuery; import com.parse.SaveCallback;  import java.util.List;  public class UpdateCourseActivity extends AppCompatActivity {      // creating variables for our edit text     private EditText courseNameEdt, courseDurationEdt, courseDescriptionEdt;      // creating variable for button     private Button updateCourseBtn, deleteCourseBtn;      // 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         deleteCourseBtn = findViewById(R.id.idBtnDelete);         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");          // Adding on click listener for delete button         deleteCourseBtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 // calling a method to delete a course.                 deleteCourse(originalCourseName);             }         });          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 deleteCourse(String courseName) {          // 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", courseName);          // on below line we are finding the course with the course name.         query.findInBackground(new FindCallback<ParseObject>() {             @Override             public void done(List<ParseObject> objects, ParseException e) {                 // if the error is null.                 if (e == null) {                     // on below line we are getting the first course and                     // calling a delete method to delete this course.                     objects.get(0).deleteInBackground(new DeleteCallback() {                         @Override                         public void done(ParseException e) {                             // inside done method checking if the error is null or not.                             if (e == null) {                                 // if the error is not null then we are displaying a toast message and opening our home activity.                                 Toast.makeText(UpdateCourseActivity.this, "Course Deleted..", Toast.LENGTH_SHORT).show();                                 Intent i = new Intent(UpdateCourseActivity.this, HomeActivity.class);                                 startActivity(i);                             } else {                                 // if we get error we are displaying it in below line.                                 Toast.makeText(UpdateCourseActivity.this, "Fail to delete course..", Toast.LENGTH_SHORT).show();                             }                         }                     });                 } else {                     // if we don't get the data in our database then we are displaying below message.                     Toast.makeText(UpdateCourseActivity.this, "Fail to get the object..", Toast.LENGTH_SHORT).show();                 }             }         });     }      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 see the output of the app. Make sure to add the course before performing this operation. 

Output:

Below is the file structure in Android Studio after performing the Delete operation:


Next Article
How to Read Data from Back4App Database in Android?

C

chaitanyamunje
Improve
Article Tags :
  • Java
  • Android
Practice Tags :
  • Java

Similar Reads

  • How to Update Data in Back4App Database in Android?
    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 u
    7 min read
  • 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 Realm Database in Android?
    In the previous series of articles on the realm database, we have seen adding, reading, and updating data using the realm database in android. In that articles, we were adding course details, reading them, and updating them. In this article, we will take a look at deleting these course details from
    6 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
  • 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 Delete Data in SQLite Database in Android using Jetpack Compose?
    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 the delete operation for deleting our items stored in the SQLite database in the android application using Jetpack Compose
    9 min read
  • How to Delete Data from Firebase Realtime Database in Android?
    In this article, we will see How to Delete added data inside our Firebase Realtime Database. So we will move towards the implementation of this deleting data in Android Firebase.   What we are going to build in this article?   We will be showing a simple AlertBox when the user long clicks on the ite
    4 min read
  • How to Debug Database in Android?
    The Android Debug Database library is a useful tool for troubleshooting databases and shared preferences in Android apps. In this article we would be looking forward to using this library and get our hand on it, so continue reading, and indulge. First thing's first, What's Exactly an Android Debug D
    3 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 Read Data from SQLite Database in Android?
    In the 1st part of our SQLite database, we have seen How to Create and Add Data to SQLite Database in Android. In that article, we have added data to our SQLite Database. In this article, we will read all this data from the SQLite database and display this data in RecyclerView. What we are going to
    12 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences