0

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

2 Answers2

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:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0
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()
}
cigien
  • 57,834
  • 11
  • 73
  • 112
  • this method can be use in firbase ui database Recyclerview – MUHAMMAD TAHA May 11 '22 at 16:42
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 12 '22 at 07:00