I have the following structure:

enter image description here

I want to delete the record in the database by clicking. How to specify the database number of the record that I want to delete. For example, I tried to delete it, but it did not work either.

Query applesQuery = FirebaseDatabase.getInstance().getReference().child("event").orderByChild("title").equalTo("TITLE"); applesQuery.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot appleSnapshot: dataSnapshot.getChildren()) { appleSnapshot.getRef().removeValue(); } } @Override public void onCancelled(DatabaseError databaseError) { Log.e(TAG, "onCancelled", databaseError.toException()); } }); 

    1 answer 1

    If I understood correctly, then your records are uploaded to the recyclerview list, at one time I did something like this:

      mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getContext(), mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { //do nothing } @Override public void onLongItemClick(View view, int position) { DatabaseReference dat = mAdapter.getRef(position); dat.removeValue(); Toast.makeText(getContext(), "Удалено", Toast.LENGTH_SHORT).show(); } })); 

    The mAdapter here is this initialized and configured FirebaseRecyclerAdapter. The processing of clicks on the recyclerview element in this example is done using the RecyclerItemClickListener class. It can be taken from here, the second answer . Add it to the project.

    • Thanks, earned. But why “crutches” with RecyclerView? If in the holder you can safely hang the listener on the press and take a position using the getAdapterPosition method. - UjinUkr
    • @UjinUkr I don’t remember why I decided to do this: D - YungBlade