我正在创建一个应用程序,以帮助用户/管理员删除记录 从firebase数据库显示在cardview上。我想要用户或管理员 删除他或她单击的特定记录,这应该删除 Firebase数据库中的对应密钥。我有寻找答案,但 那些只删除数据库中的所有键。 下面是我的代码:
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<RecyclerViewAdapterAdmin.ViewHolder> {
// context variable and a list of the Rooms
Context mCtx;
List<Rooms> roomsList;
// Parametarized constructor
public RecyclerViewAdapterAdmin(Context mCtx, List<Rooms> 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<Void>() {
@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);
}
}
}
答案 0 :(得分:0)
您可以通过如下引用直接从Firebase中删除该节点:-
databaseReference.child("room_node").removeValue();
,然后可以通过notifyItemRemoved(position)
更新适配器