Mehul Kanzariya
Mehul Kanzariya

Reputation: 1348

Unable to delete both item and value Firebase RecyclerView

I am using Firebaseui's recyclerview to show values in cart which is a RecyclerView. I have delete button in each row of recyclerview and when i click on delete button it should delete value from Firebase's database and also row in recyclerview should get deleted. But problem is when i click anywhere on recyclerview both item and database value gets deleted.
Here is the onClick code i am using :

  recyclerViewCart.addOnItemTouchListener(
            new RecyclerItemClickListener(Cart.this, recyclerViewCart, new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View childView, int position) {
                    Log.v(TAG, "YouClickon" + position);

                    key = mRecyclerViewAdapter.getRef(position).getKey();


                    listRef.child(key).removeValue();


                }
            }) {

            }
    );    

I want to perform this operation only when user clicks on delete button. How should i do this. Please help.

Upvotes: 1

Views: 447

Answers (2)

My Will
My Will

Reputation: 410

it must be:

recyclerViewCart.getRef(position).removeValue();

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Your click handler is wired to the entire item, so it will indeed run whenever that item is clicked.

If you want to only delete the item when the user clicks the button, you should wire up the click handler to that button. You can do this in your populateViewHolder() method:

public void populateViewHolder(ChatHolder chatMessageViewHolder, Chat chatMessage, int position) {
    chatMessageViewHolder.getDeleteButton().setOnClickListener(...).
}

Upvotes: 1

Related Questions