我正在使用Recycle Adapter Class在我的应用中显示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);
使用此代码:
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();
}
});
您可以将null
放在bookid上,以便从数据库中删除它。
dbreference.child("books").child(bookId).setValue(null);
如果您知道该书的ID,则应该能够删除具有该ID引用的子项。
你必须像之前用holder.bookCall.setOnClickListener(...)那样实现点击监听器;
码:
holder.delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dbreference.child("books").child(bookId).remove();
}
});
我希望它对你有所帮助。
此代码将删除您想要的书,只需获取该书的ID并放在onClickListener中:
dbreference.child("books").child(item).child(bookid).removeValue();
dbref.child("books").child("Computer Science").child(key).removeValue();