'get id of node in firebase by item in recyclerView

enter image description here

i want to access on this point by clicking on element inside cardView i only can reach to the point before it by using this line

 reference = FirebaseDatabase.getInstance().getReference("Posts")
            .child(FirebaseAuth.getInstance().getCurrentUser().getUid());
    String str =  reference.getKey();


Solution 1:[1]

To get the keys of the child nodes, you'll need to load the reference and then loop over snapshot.getChildren(). So something like:

reference.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
            System.out.println(postSnapshot.getKey());
            ...
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}

If you only want to retrieve one specific node, you'll need to know the key of that node. Typically this means that you need to keep the key of each snapshot when you read the data, and associate that with the position of each node in the recycler view. The once the user clicks on an item in the view, you look up the key based on the position they clicked, and can get that item from the database again with:

reference.child(key).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        System.out.println(dataSnapshot.getKey());
        ...
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException();
    }
}

For some more on this, see:

Solution 2:[2]

FirebaseRecyclerOptions<Order> options = new FirebaseRecyclerOptions.Builder<Order()
                        .setQuery(query, Order.class)
                        .build();

adapter = new FirebaseRecyclerAdapter<Order, myOrderRecyclerViewHolder.myViewHolder>(options)
{
 String key =options.getSnapshots().getSnapshot(position).getKey()
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 cigien