Vincent Macharia Vincent Macharia - 1 month ago 26
Android Question

This Firebase query(orderbyKey) cannot display anything

I am trying to fetch data from child("REPORTS")where datasnapshot.getRef is the same as key passed from getIntent,there is no display and I cannot tell what is wrong with code syntax

Intent i = getIntent();
final String key = i.getExtras().getString("firekey");

Query queryRef = mDatabase.child("REPORTS").equalTo(key).orderByKey();
queryRef.addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.d( "Post", "data");
Map<String, Object> newPost = (Map<String, Object>) dataSnapshot.getValue();
if (newPost != null) {
reportSingle = new FoodItem(newPost.get("MEAL_TITLE").toString().trim(),
newPost.get("DESCRIPTION").toString().trim(),
newPost.get("MEAL_INGREDIENTS").toString().trim(),

Answer

It looks like you're trying to load a single report. In that case, you'll need:

DatabaseReference reportRef = mDatabase.child("REPORTS").child(key);
reportRef.addValueEventListener(new ValueEventListener() {
  public void onDataChanged(DataSnapshot dataSnapshot) {
    Log.d( "Post", "data");
    if (dataSnapshot.exists()) {
      reportSingle = new FoodItem(
        dataSnapshot.child("MEAL_TITLE").getValue(String.class).trim(),
        dataSnapshot.child("DESCRIPTION").getValue(String.class).trim(),
        dataSnapshot.child("MEAL_INGREDIENTS").getValue(String.class).trim(),