无法从Firebase数据库读取嵌套数据 [英] Failing to Read Nested Data From Firebase Database

查看:58
本文介绍了无法从Firebase数据库读取嵌套数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发送开胃小吃"在一个活动1到活动2的 name 键中.

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

在活动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);

输出为:

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.

我只想知道,我如何到达 recipes 节点.我尝试到现在为止:

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));

Log.e("data",String.valueOf(datasnapshot))给出以下输出:

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}} }

我的DetailModel类是一个简单的类:

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.

如果(dataSnapshot.exists()){并且没有执行任何内部代码,则不会到达.由于我还检查了设置日志,因此在Logcat中也找​​不到任何内容.任何提示,为什么会这样?

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.

推荐答案

代替此:

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

您将要使用:

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

所以:

  1. 使用 orderByChild("name")告诉数据库对所有子节点的 name 属性值进行排序.
  2. 使用 equalTo(...)来过滤排序的数据.
  3. 删除 getRef(),因为这实际上会撤消所有查询内置.
  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);
        }
    }

所以:

  1. 收听我们刚刚创建的整个查询.
  2. 然后循环遍历快照中返回的每个节点的 recipes 的子代.

这篇关于无法从Firebase数据库读取嵌套数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆