Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

How to Delete data in Firebase Realtime Database

How to Delete data in Firebase Realtime Database

  • Create and Insert in Database Click Here
  • Retrieve From database and show it on ListView Click Here
  • Update data in Database Click Here

To Delete Data we need to use on item click listener on our ListView which we created.

When we click on the ListView item, it will return us a unique id that is created on our database at the time of the creation of a node. then on behalf of that unique specific node id, we will delete the specific value in the database.

Also, we create an Alert Dialog box with a delete button to delete our value.

 package studio.harpreet.firebasedatabase;  
 import androidx.annotation.NonNull;  
 import androidx.appcompat.app.AlertDialog;  
 import androidx.appcompat.app.AppCompatActivity;  
 import android.content.DialogInterface;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.AdapterView;  
 import android.widget.ArrayAdapter;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.LinearLayout;  
 import android.widget.ListView;  
 import android.widget.TextView;  
 import com.google.firebase.database.DataSnapshot;  
 import com.google.firebase.database.DatabaseError;  
 import com.google.firebase.database.DatabaseReference;  
 import com.google.firebase.database.FirebaseDatabase;  
 import com.google.firebase.database.ValueEventListener;  
 import java.util.ArrayList;  
 import java.util.List;  
 public class MainActivity extends AppCompatActivity {  
   DatabaseReference databasestudent;  
   EditText roll_et,name_et,std_et;  
   Button add_btn;  
   ListView studentListView;  
   List<StudentModel > studentlist;  
   protected void onCreate(Bundle savedInstanceState) {  
     roll_et = findViewById(R.id.student_roll);  
     name_et = findViewById(R.id.student_name);  
     std_et = findViewById(R.id.student_std);  
     add_btn = findViewById(R.id.add_btn);  
     studentListView = findViewById(R.id.student_listview);  
     studentlist = new ArrayList<>();  
     databasestudent = FirebaseDatabase.getInstance().getReference("Student");  
     add_btn.setOnClickListener(new View.OnClickListener() {  
                     public void onClick(View v) {  
     studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
       public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {  
         final StudentModel studentModel = studentlist.get(position);  
         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);  
         LinearLayout layout = new LinearLayout(MainActivity.this);  
         final EditText name_et = new EditText(MainActivity.this);  
         builder.setTitle("Update Name")  
             .setMessage("Write name to Update")  
             .setPositiveButton("Update", new DialogInterface.OnClickListener() {  
               public void onClick(DialogInterface dialog, int which) {  
                 DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Student").child(studentModel.getId());  
                 StudentModel studentModel1 = new StudentModel(studentModel.getId(),studentModel.getRollno(),  
         }).setNegativeButton("Delete", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Student").child(studentModel.getId());

 private void addstudent(String Rollno, String Name, String Std)  
     String id = databasestudent.push().getKey();  
     StudentModel studentModel = new StudentModel(id,Rollno,Name,Std);  
   protected void onStart() {  
     databasestudent.addValueEventListener(new ValueEventListener() {  
       public void onDataChange(@NonNull DataSnapshot dataSnapshot) {  
         studentlist.clear();  // because everytime when data updates in your firebase database it creates the list with updated items  
                     // so to avoid duplicate fields we clear the list everytime  
           for(DataSnapshot studentsnapshot : dataSnapshot.getChildren())  
             StudentModel studentModel = studentsnapshot.getValue(StudentModel.class);  
           Student_ListAdaptor adaptor = new Student_ListAdaptor(MainActivity.this,studentlist);  
       public void onCancelled(@NonNull DatabaseError databaseError) {  

It will successfully delete data in our Firebase Realtime Database and show it on a ListView

Follow us for more posts like this, 
Subscribe to Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments