2

enter image description here

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.

Sainita
  • 332
  • 1
  • 4
  • 16

2 Answers2

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.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Sir, there is no data Displaying in the App. I think the mistake is here : dataSnapshot.child("recipes"). recipes is node the child of name , I think so. Please fix this Sir, I request – Sainita Jan 16 '21 at 14:23
  • "Sir, there is no data Displaying in the App." is too broad to help with here. Set some breakpoints in the code, run it in a debugger, and step through it. At that point: what lines do what you expect them to do, and which one line doesn't do what you expect? Does it reach `if (dataSnapshot.exists()) {`? Does it go into the `for (DataSnapshot dataSnapshot1 : dataSnapshot.child("recipes").getChildren()) {`? Does it ` Log.e("data", String.valueOf(dataSnapshot))`? – Frank van Puffelen Jan 16 '21 at 15:20
  • No Sir, 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 ? – Sainita Jan 16 '21 at 15:23
  • The only Log available is at the Top, ID received is: Appetizers and Snacks . Besides, there is nothing in Logcat. @Frank van Puffelen – Sainita Jan 16 '21 at 15:32
  • Note that by now you've changed your original question significantly, making part of my answer irrelevant. In the future please provide such edits as additions to your question, so that future visitors to this post still can see the entire history at a glance - and answers don't become outdated. – Frank van Puffelen Jan 16 '21 at 15:50
  • If the program never goes into `onDataChange` it makes sense that the rest of the code doesn't work either. If your `onCancelled` also doesn't get called, the typical cause is that the code is not connected to the database. – Frank van Puffelen Jan 16 '21 at 15:52
  • I am just getting knowledge from you Sir. I have just started Learning firebase database and appreciate every help in this Direction :) . That's the question , still unable to figure out the Issue ! – Sainita Jan 16 '21 at 15:53
  • Sorry Sir, It did Now, after I uniinstalled and Reinstalled the App. This Log executes : public void onDataChange(@NonNull DataSnapshot dataSnapshot) { Log.e("Data REC", String.valueOf(dataSnapshot)); . Here the complete node is visible in logcat , but not the Log inside the for Loop, its not executed Sir. – Sainita Jan 16 '21 at 15:54
  • If you `Log.e("data", String.valueOf(dataSnapshot));` right inside of `onDataChange` what does it show? My guess is that it doesn't have a `recipes` child, as otherwise the debugger would step into the `for` loop. – Frank van Puffelen Jan 16 '21 at 16:28
  • This is the Output when Sir after i put Log right inside of onDataChange : 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}} } – Sainita Jan 16 '21 at 17:09
  • 1
    Please show the updated code and its output in your question, instead of in a comment here. That also gives you the chance to format it. – Frank van Puffelen Jan 16 '21 at 17:14
  • I have posted updated Code. Please check @Frank – Sainita Jan 16 '21 at 17:35
  • 1
    Ah, I see it. Since we're querying categories and then loop over recipes, we need two nested loops. Check the updated code, and the names I've given to the variables. Using meaningful names (as opposed to `dataSnapshot1`) makes it much easier to spot a problem such as this. – Frank van Puffelen Jan 16 '21 at 17:44
  • FIXED . Such a Stress it was !! . Thank You Sir for Your Time :) – Sainita Jan 16 '21 at 17:48
0
    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)
Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50