Failing to Read Nested Data From Firebase Database

狂风中的少年 提交于 2021-01-20 17:00:52

问题


I am sending "Appetizers and Snacks" which is in name key from one activity 1 to activity 2.

In activity 2,the data received is simply :

intent = getIntent();
String received_id = intent.getStringExtra("cat_id");
Log.e("ID received is", received_id);

Output is :

ID received is : Appetizers and Snacks

With the Help of this value, I'm trying to read the Node recipes and display all the data in their Respective Views.

I only want to know , how can I get to the recipes node. I tried this till now :

  query = FirebaseDatabase.getInstance().getReference("All Categories").orderByChild("name").equalTo(received_id);

  query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Log.e("data", String.valueOf(dataSnapshot));
            if (dataSnapshot.exists()) {
                for (DataSnapshot dataSnapshot1 : dataSnapshot.child("recipes").getChildren()) {
                    Log.e("data final is ", String.valueOf(dataSnapshot1.getValue()));
                    DetailModel p = dataSnapshot1.getValue(DetailModel.class);
                    detailModelList.add(p);
                }

                detailAdapter = new DetailAdapter(DetailCategory.this, detailModelList);
                recyclerView.setAdapter(detailAdapter);
                progressBar.setVisibility(View.INVISIBLE);
            } else {
                Toast.makeText(DetailCategory.this, "No data available !", Toast.LENGTH_SHORT).show();
                Log.e("No data available !", "No data available !");
                progressBar.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

            Toast.makeText(DetailCategory.this, databaseError.getDetails(), Toast.LENGTH_SHORT).show();
        }
    });
    recyclerView.setLayoutManager(new GridLayoutManager(DetailCategory.this, 2));

The Log.e("data", String.valueOf(datasnapshot)) gives this Output :

DataSnapshot { key = All Categories, value = {0={image=https://i.imgur.com/0toOJMa.jpg, recipes={randomkey={image=https://i.ibb.co/gvH8Vp2/5f6fg0l8-keraal-roast-chicken-62.png, servings=4 servings, ingredients="complete Ingredients here"}}, name=Appetizers and Snacks}} }

My DetailModel class is a simple class :

public DetailModel() {
}

String image,title, time,servings,ingredients,steps;
 public DetailModel(Integer id, String image, String title, String time, String servings, String ingredients, String steps) {
    this.id = id;
    this.image = image;
    this.title = title;
    this.time = time;
    this.servings = servings;
    this.ingredients = ingredients;
    this.steps = steps;
}

public Integer getId() {
    return id;
}

public String getImage() {
    return image;
}

public String getTitle() {
    return title;
}

public String getTime() {
    return time;
}

public String getServings() {
    return servings;
}

public String getIngredients() {
    return ingredients;
}

public String getSteps() {
    return steps;
}
   

But every time there is no data Received in the App, Completely Blank, even the Else conditions are not executing.

it doesn't reach if (dataSnapshot.exists()) { and none of the code inside is executed. Since, I also checked with settings logs , nothing is found in Logcat as well . Any Tips , why this is happening ?

EDIT : i did as Frank Recommended, Still there is no data in the App

Please Guide me reading this nested Data.


回答1:


Instead of this:

databaseReference = FirebaseDatabase.getInstance().getReference("All Categories").child("name").orderByValue().equalTo(received_id).getRef();

You'll want to use:

Query query = FirebaseDatabase.getInstance().getReference("All Categories").orderByChild("name").equalTo(received_id);

So:

  1. Use orderByChild("name") to tell the database to order all child nodes on the value of their name property.
  2. Use equalTo(...) to then filter down the sorted data.
  3. Remove the getRef() as that actually undoes all your query buildin.

To then read the data, do:

query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            for (DataSnapshot categorySnapshot: dataSnapshot.getChildren()) {
                for (DataSnapshot recipeSnapshot: categorySnapshot.child("recipes").getChildren()) {
                    DetailModel p = recipeSnapshot.getValue(DetailModel.class);
                    detailModelList.add(p);
                }
            }

            detailAdapter = new DetailAdapter(DetailCategory.this, detailModelList);
            recyclerView.setAdapter(detailAdapter);
            progressBar.setVisibility(View.INVISIBLE);
        } else {
            Toast.makeText(DetailCategory.this, "No data available !", Toast.LENGTH_SHORT).show();
            progressBar.setVisibility(View.INVISIBLE);
        }
    }

So:

  1. Listen to the entire query we just created.
  2. Then loop over the children of recipes of each node we get back in the snapshot.



回答2:


    databaseReference = FirebaseDatabase.getInstance().getReference();
    val ref = database.child("All Categories").child("0").child("recipes")
    
   
    val valueEventListener = object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
         val recipes = dataSnapshot.getValue(Recipes::class.java) // recipes model reading
    }

    override fun onCancelled(databaseError: DatabaseError) {
        
    }
}
ref.addListenerForSingleValueEvent(valueEventListener)


来源:https://stackoverflow.com/questions/65740823/failing-to-read-nested-data-from-firebase-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!