title: How to delete a specific record displayed on a cardview from Firebase Realtime Database when user clicks on a cardview.
body: <p>I am creating an application to help user / admin to delete a record 
  displayed on a cardview from firebase database. And i want the user or Admin 
  to delete the specific record he or she clicks on and that should delete the 
  corresponding key in firebase database . I  have search for answers but 
  those one only delete all keys in the database.
  Below is my code:</p>
  
  <pre><code>package io.zentechhotelbooker.adapters;
  
  import android.app.AlertDialog;
  import android.app.ProgressDialog;
  import android.content.Context;
  import android.content.DialogInterface;
  import android.support.annotation.NonNull;
  import android.support.v7.widget.CardView;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.ImageView;
  import android.widget.ProgressBar;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.bumptech.glide.Glide;
  import com.google.android.gms.tasks.OnCompleteListener;
  import com.google.android.gms.tasks.Task;
  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 com.google.firebase.storage.StorageReference;
  
  import java.util.List;
  
  import io.zentechhotelbooker.R;
  import io.zentechhotelbooker.models.Rooms;
  
  import static io.zentechhotelbooker.activities.AddRoomsActivity.Database_Path;
  
  public class RecyclerViewAdapterAdmin extends RecyclerView.Adapter&lt;RecyclerViewAdapterAdmin.ViewHolder&gt; {
  
      // context variable and a list of the Rooms
       Context mCtx;
       List&lt;Rooms&gt; roomsList;
  
      // Parametarized constructor
      public RecyclerViewAdapterAdmin(Context mCtx, List&lt;Rooms&gt; roomsList ){
          this.mCtx = mCtx;
          this.roomsList = roomsList;
  
      }
  
      @NonNull
      @Override
      public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  
          View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items,parent,false);
  
          ViewHolder viewHolder = new ViewHolder(view);
  
          return viewHolder;
      }
  
      @Override
      public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
  
          // getting the position of each room and its details
          final Rooms rooms = roomsList.get(position);
  
          /** getting the room details and setting
           * it to the their respective view objects
           * or views
           */
          holder.room_number.setText(" Room Number : " + rooms.getRoom_number());
          holder.room_price.setText(" Price : GHC " + rooms.getPrice());
  
          //Loading image into Glide using the glide library.
          Glide.with(mCtx).load(rooms.getRoom_image()).into(holder.room_image);
  
          // on Click listener for the CardView
          holder.cardView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  // Create an an alert builder
                  AlertDialog.Builder builder = new AlertDialog.Builder(mCtx);
                  // Sets a Title and a Message on it
                  builder.setTitle("Delete Room");
                  builder.setMessage("Are you sure you want to delete this room");
                  builder.setCancelable(false);
  
                  // sets the positive button on the Alert Dialog
                  builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
  
                          // displays the progress bar
                          holder.progressBar.setVisibility(View.VISIBLE);
  
                          int selectedItem = position;
  
                          // Creating StorageReference and DatabaseReference object.
                          final DatabaseReference  databaseReference =
                                  FirebaseDatabase.getInstance().getReference(Database_Path);
  
  
                          databaseReference.addValueEventListener(new ValueEventListener() {
                              @Override
                              public void onDataChange(DataSnapshot dataSnapshot) {
                                  // code to delete room from database
                                  for(DataSnapshot deleteSnapshot : dataSnapshot.getChildren()){
                                      Rooms rooms = deleteSnapshot.getValue(Rooms.class);
                                      // delete selected room from database
                                     deleteSnapshot.getRef().removeValue();
                                  }
                                  // File deleted successfully message
                                  Toast.makeText(mCtx," Room deleted Successfully",Toast.LENGTH_LONG).show();
                              }
  
                              @Override
                              public void onCancelled(DatabaseError databaseError) {
                                  // display error message
                                  Toast.makeText(mCtx,databaseError.getMessage(),Toast.LENGTH_LONG).show();
                              }
                          });
  
  
                          // Room deleted successfully message
                          Toast.makeText(mCtx,rooms.getRoom_number() + " deleted Successfully",Toast.LENGTH_LONG).show();
  
                          // getting the ImageUrl from the Rooms class
                          /*StorageReference imageRef = FirebaseStorage.getInstance().getReferenceFromUrl(rooms.getRoom_image());
  
                          imageRef.delete().addOnSuccessListener(new OnSuccessListener&lt;Void&gt;() {
                              @Override
                              public void onSuccess(Void aVoid) {
  
                                  // hides the progressBar
                                  holder.progressBar.setVisibility(View.GONE);
  
                                  // Getting image upload ID.
                                  String ImageUploadID = holder.databaseReference.getKey();
  
                                  // removes values from the database
                                  holder.databaseReference.child(ImageUploadID).removeValue();
  
                                  // File deleted successfully message
                                  Toast.makeText(mCtx,rooms.getRoom_number() + " deleted Successfully",Toast.LENGTH_LONG).show();
                              }
                          }).addOnFailureListener(new OnFailureListener() {
                              @Override
                              public void onFailure(@NonNull Exception e) {
                                  // File could not be deleted successfully
                                  Toast.makeText(mCtx,e.getMessage(),Toast.LENGTH_LONG).show();
                              }
                          });
                          */
                      }
                  });
  
                  // sets the positive button on the Alert Dialog
                  builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialogInterface, int i) {
                          // dismiss the Alert Dialog
                          dialogInterface.dismiss();
                      }
                  });
  
                  // Creates a new AlertDialog and displays it
                  AlertDialog alertDialog = builder.create();
                  alertDialog.show();
  
              }
          });
  
  
      }
  
      // class to return the size of the rooms added to the database
      @Override
      public int getItemCount() {
          return roomsList.size();
      }
  
      public class ViewHolder extends RecyclerView.ViewHolder{
  
          // Creating object of the classes
          RecyclerView recyclerView;
          CardView cardView;
          ImageView room_image;
          TextView room_number;
          TextView room_price;
  
         // Creating object of the classes
          ProgressBar progressBar;
  
          DatabaseReference databaseReference;
  
          // constructor
          public ViewHolder(View itemView){
              super(itemView);
  
              // settings ids to those in the recyclerview_list.xml file
  
              recyclerView = itemView.findViewById(R.id.recyclerView);
  
              cardView = itemView.findViewById(R.id.room_cardView);
  
              room_image = itemView.findViewById(R.id.room_image);
              room_number = itemView.findViewById(R.id.room_number);
              room_price = itemView.findViewById(R.id.room_price);
  
              progressBar = itemView.findViewById(R.id.progressBar);
  
             // initializing the database reference
              databaseReference = FirebaseDatabase.getInstance().getReference(Database_Path);
  
  
          }
  
      }
  
  }
  </code></pre>