Reputation: 84
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();
Upvotes: 0
Views: 379
Reputation: 1
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()
}
Upvotes: 0
Reputation: 598728
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:
Upvotes: 1