gpt4 book ai didi

android - 无法从 Firebase 数据库读取嵌套数据

转载 作者:行者123 更新时间:2023-12-04 03:42:26 25 4
gpt4 key购买 nike

enter image description here
我正在发送“开胃菜和小吃”,位于 name从一项 Activity 1 到 Activity 2 的键。
在 Activity 2 中,收到的数据很简单:

intent = getIntent();
String received_id = intent.getStringExtra("cat_id");
Log.e("ID received is", received_id);
输出是:
ID received is : Appetizers and Snacks
在这个值的帮助下,我试图读取节点 recipes并在各自的 View 中显示所有数据。
我只想知道,我怎样才能到 recipes节点。我试过这个直到现在:
  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;
}

但是每次在App中没有接收到数据,完全空白,连Else条件都不执行。
它不会到达 if (dataSnapshot.exists()) { 并且没有执行任何内部代码。因为,我还检查了设置日志,在 Logcat 中也没有找到任何内容。任何提示,为什么会这样?
编辑:我按照弗兰克的推荐做了,应用程序中仍然没有数据
请指导我阅读此嵌套数据。

最佳答案

取而代之的是:

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);
所以:
  • 使用 orderByChild("name")告诉数据库根据其 name 的值对所有子节点进行排序属性(property)。
  • 使用 equalTo(...)然后过滤排序后的数据。
  • 删除 getRef()因为这实际上会撤消您的所有查询构建。

  • 然后读取数据,请执行以下操作:
    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);
    }
    }
    所以:
  • 收听我们刚刚创建的整个查询。
  • 然后循环遍历 recipes 的 child 我们在快照中返回的每个节点。
  • 关于android - 无法从 Firebase 数据库读取嵌套数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65740823/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com