Peter Valek
Peter Valek

Reputation: 87

Firebase remove children from RecyclerView Adapter

I need to remove child from Firebase after onClick from RecyclerView Adapter.

I have something like this:

public void onBindViewHolder(final ViewHolder holder, final int position) {
  holder.fromTextView.setText(my_data.get(position).getFromName());

  holder.fromTextView.setOnClickListener(new View.OnClickListener() {

  @Override
  public void onClick(View view) {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference messageRef = database.getReference();
    final DatabaseReference mess = messageRef.child("notifications").child(toId.toString());

    mess.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            //Code for delete?
        }
   }
  }
  
}

Firebase Database: Firebase Database Android RecyclerView: Android Recycler View

After click on item, I need to delete it from Firebase Database. I need to delete only one child.

Thanks.

Upvotes: 1

Views: 3343

Answers (3)

RileyManda
RileyManda

Reputation: 2641

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:

 @Override
    protected void onCreate(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:

final DatabaseReference itemRef = 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:

final String myKey = 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
@Override
public GameViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
return new GameViewHolder(inflater.inflate(R.layout.single_item, viewGroup, false));}

@Override
protected void onBindViewHolder(@NonNull GameViewHolder viewHolder,int position,
@NonNull final Game model){
final DatabaseReference itemRef = getRef(position);              
final String myKey = itemRef.getKey();//you can name the myKey whatever you want.


 viewHolder.delBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
  mDatabase.child(myKey).removeValue();

                    }
                });

More details from this blog

Firebase Official Documents

Upvotes: 4

Arnav Rao
Arnav Rao

Reputation: 6992

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");
                        }
                    }
                });

Upvotes: 0

Alex Mamo
Alex Mamo

Reputation: 138824

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();

Upvotes: 0

Related Questions