我正在使用回收适配器类在我的应用程序中显示Firebase数据,其中包含可购买的书籍列表。
但是一本书售出后,我希望用户能够删除该书,并最终通过onClick of Button从Firebase数据库中删除。 我该怎么办?
这是我的Firebase数据结构:
这是我的适配器数据类:
public class SubjectBooksAdapter extends RecyclerView.Adapter<SubjectBooksAdapter.MyViewHolder> {
ArrayList<Books> bookslist;
CardView cv;
FirebaseAuth fauth;
FirebaseDatabase database;
DatabaseReference dbreference;
Books b;
public SubjectBooksAdapter(ArrayList<Books> bookslist){
this.bookslist = bookslist;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout,parent,false);
return new MyViewHolder(v);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView bookName,bookAuthor,bookDesc,bookPrice,bookCall;
ImageView iv;
Button delete;
MyViewHolder(final View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.my_card_view);
iv = (ImageView) itemView.findViewById(R.id.imageView);
database = FirebaseDatabase.getInstance();
dbreference = database.getReference("books");
bookName = (TextView) itemView.findViewById(R.id.bookName);
bookAuthor = (TextView) itemView.findViewById(R.id.bookAuthor);
bookDesc = (TextView) itemView.findViewById(R.id.bookDesc);
bookPrice = (TextView) itemView.findViewById(R.id.bookPrice);
bookCall = (TextView) itemView.findViewById(R.id.bookCall);
fauth = FirebaseAuth.getInstance();
delete = (Button) itemView.findViewById(R.id.delete);
}
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
database = FirebaseDatabase.getInstance();
dbreference = database.getReference("books");
b = bookslist.get(position);
holder.bookName.setText(b.getBname());
holder.bookAuthor.setText(b.getBauthor());
holder.bookDesc.setText(b.getBdesc());
holder.bookPrice.setText("Rs. "+b.getPrice());
holder.bookCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Log.e("Current user is ", fauth.getCurrentUser().getEmail());
b = bookslist.get(position);
String[] arr = {b.getSelleremail(),b.getSellername(),b.getBname(),b.getBauthor()};
//Log.e("Seller is ",b.getSellername());
Intent in = new Intent(v.getContext(),Chat.class);
in.putExtra("seller",arr);
v.getContext().startActivity(in);
}
});
Glide.with(cv.getContext()).load(Uri.parse(b.getPics())).placeholder(R.drawable.bshelf).error(R.drawable.bshelf).into(holder.iv);
}
@Override
public int getItemCount() {
return bookslist.size();
}
}
我正在上载类中使用此命令将数据推送到Firebase中:
String bookid = dbreference.child("books").child(item).push().getKey();
dbreference.child("books").child(item).child(bookid).setValue(b);
答案 0 :(得分:2)
如果您知道那本书的ID,则应该可以删除带有ID参考的孩子。
您必须像以前一样使用 holder.bookCall.setOnClickListener(...);
来实现点击侦听器代码:
n
我希望它能对您有所帮助。
答案 1 :(得分:2)
您可以将null
放在您的书夹上,以将其从数据库中删除。
dbreference.child("books").child(bookId).setValue(null);
答案 2 :(得分:1)
使用此代码:
DatabaseReference driverRef = FirebaseDatabase.getInstance().getReference().child("bname");
driverRef.removeValue();
if you delete whole database
use this
DatabaseReference driverRef = FirebaseDatabase.getInstance().getReference();
driverRef.removeValue();
//---------------------Use on Button click Listener--------
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatabaseReference driverRef = FirebaseDatabase.getInstance().getReference().child("bname");
driverRef.removeValue();
}
});
答案 3 :(得分:1)
此代码将删除您想要的一本书,只需获取该书的ID并将其放置在onClickListener中:
dbreference.child("books").child(item).child(bookid).removeValue();
答案 4 :(得分:1)
dbref.child("books").child("Computer Science").child(key).removeValue();