通过查询检索子节点有点麻烦。
这是我的数据结构:
我设法能够使用上图中突出显示的“1”节点成功查询和检索数据。这是我的代码:
databaseRecipes = FirebaseDatabase.getInstance().getReference().child("recipe");
Query query = databaseRecipes.orderByChild(userId).equalTo(true);
query.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(dataSnapshot.exists())
{
Toast.makeText(getApplicationContext(), "Results found!", Toast.LENGTH_LONG).show();
arrayList.clear();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
{
Recipe recipe = dataSnapshot1.getValue(Recipe.class);
arrayList.add(recipe);
}
final RecipeAdapter recipeAdapter = new RecipeAdapter(getApplicationContext(), arrayList);
recyclerView.setAdapter(recipeAdapter);
recipeAdapter.notifyDataSetChanged();
}
else
{
// Toast.makeText(getApplicationContext(), "No retreivable results :(", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), userId, Toast.LENGTH_LONG).show();
}
}
但是,我宁愿将所有“收藏夹”放在它们自己的单独节点下,如用“2”突出显示的那样。(那里只有一个孩子用于测试目的,实际上会节省更多,所以这样做会更整洁。)
使用此代码:
Query query = databaseRecipes.child("favourite").orderByChild(userId).equalTo(true);
我还尝试将引用更改为:
databaseRecipes = FirebaseDatabase.getInstance().getReference().child("recipe").child("favourite");
并查询:
Query query = databaseRecipes.orderByChild(userId).equalTo(true);
但无论哪种方式都不会检索到任何结果。我认为我的问题是从以下方面要求孩子DataSnapshot:
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren())
但是我整天都在兜圈子,试图找到一个替代方案(我想我错过了一些非常简单的东西——这更烦人!),所以任何帮助都将不胜感激。
提前为我糟糕的绘画技巧道歉!
慕工程0101907
相关分类