Firebase Remove Children From Recyclerview Adapter
Solution 1:
Recap:
Suppose you have a Database reference responsible for fetching your entire list of items called: mDatabase,which you should have already innitialized:
private DatabaseReference mDatabase;
And in your activity main method:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
mDatabase = FirebaseDatabase.getInstance().getReference();
And fetching your Data as:
mDatabase = FirebaseDatabase.getInstance().getReference()
.child("myitems").child(someKey);
Now that your data is displaying in your recycler view,at some point we will use our mDatabase to successfully delete an item below:
In your ViewHolder:
You have to first get a reference of the position of your item from your ViewHolder which is directly referencing the item from the database:
Assuming your Database reference is itemRef:
final DatabaseReference ** itemRef**
You would have to assign the database reference to the position of each item by using getRef:
finalDatabaseReferenceitemRef= getRef(position);
And there after use the reference to your position and database reference and assign it to a String which you will use to get the unique key of your item:
finalStringmyKey= itemRef.getKey();//you can name the myKey whatever you want.
finally we can access the key to delete an item:
In this example:Lets assume your delete button is:delBtn,and your ViewHolder is GameViewHolder viewHolder as below:
@Override
protected void onBindViewHolder(@NonNull GameViewHolder viewHolder,int position,
@NonNull final Game model)
Deleting an Item:
In your ViewHolder/referencing the viewHolder...we set an o'clock listener on our button and use our database reference for all items to get the particular items position and remove the value:
viewHolder.delBtn.setOnClickListener(v -> {
mDatabase.child(myKey).removeValue();
Our Entire deleting item code would therefore look like this:
@NonNull@Overridepublic GameViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflaterinflater= LayoutInflater.from(viewGroup.getContext());
returnnewGameViewHolder(inflater.inflate(R.layout.single_item, viewGroup, false));}
@OverrideprotectedvoidonBindViewHolder(@NonNull GameViewHolder viewHolder,int position,
@NonNullfinal Game model){
finalDatabaseReferenceitemRef= getRef(position);
finalStringmyKey= itemRef.getKey();//you can name the myKey whatever you want.
viewHolder.delBtn.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
mDatabase.child(myKey).removeValue();
}
});
Solution 2:
User removeValue method and add listener to know the status of the delete operation as shown below. For complete example http://www.zoftino.com/firebase-realtime-database-android-example#realtime-database-delete
FirebaseDatabase.getInstance().getReference()
.child("notifications").child(toId.toString()).removeValue()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("Delete", "Notification has been deleted");
} else {
Log.d("Delete", "Notification couldn't be deleted");
}
}
});
Solution 3:
In order to delete a Firebase database record, you don't need to use a listener, you only need to use removeValue()
method directly on the reference like this:
messageRef.child("notifications").child(toId.toString()).removeValue();
Post a Comment for "Firebase Remove Children From Recyclerview Adapter"