How To Retrieve My Data Inside The Random Key?
I want to get the data inside the random key, as shown here: I am using Firebase and I have tried a lot of things, but I cannot solve this. public class MainActivity extends AppCo
Solution 1:
You're creating an adapter on Requests
. That means that the recycler view will show a list of requests: so one row for each request. But you created a data class Food
and a FoodViewHolder
, so it seems you actually want to display a list of foods.
To display a list of foods in your recycler view, you must have a list of foods in your database. For example, you can show the foods for the first request by determining the DatabaseReference
to that request and then showing that in the adapter:
DatabaseReferencerequestsRef= FirebaseDatabase.getInstance().getReference().child("Requests");
QueryfirstRequestQuery= requestsRef.orderByKey().limitToFirst(1);
firstRequestQuery.addListenerForSingleValueEvent(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot requestSnapshot: dataSnapshot.getChildren()) {
FirebaseRecyclerAdapter<Food, FoodViewHolder> adapter = newFirebaseRecyclerAdapter<Food, FoodViewHolder>(
Food.class,
R.layout.individual_row,
FoodViewHolder.class,
requestSnapshot.getRef().child("foods")
) {
@OverrideprotectedvoidpopulateViewHolder(FoodViewHolder viewHolder, Food model, int position) {
viewHolder.setProductName(model.getProductName());
viewHolder.setQuantity(model.getQuantity());
}
};
recyclerView.setAdapter(adapter);
}
}
@OverridepublicvoidonCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
}
While there may be some problems with the code, it demonstrates how to show the foods of the first request in the recycler view. The key is requestSnapshot.getRef().child("foods")
, which ensures the adapter loads the foods of the first request.
Post a Comment for "How To Retrieve My Data Inside The Random Key?"