【发布时间】:2017-11-12 19:49:24
【问题描述】:
我开始使用数据库连接编写 Android 应用程序。作为 IDE,我使用 Android Studio 和 Firebase 作为数据库。这是我在 Firebase 中的数据结构:
在我的应用程序中,我需要用所有名称填写 ListView。所以我想要来自 UUID 孩子的孩子“Rezepte”的三个孩子的数据。我希望这是可以理解的。
我在谷歌上搜索了很多,但我无法弄清楚它是如何工作的。我当前版本的java代码是这样的:
final FirebaseUser user = firebaseAuth.getCurrentUser();
// trying to get the reference of the right path
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(user.getUid()).child("Rezepte");
ref.addListenerForSingleValueEvent(
new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// calling a method which should read the data from the snapshot and put it into the ListView
fillList((Map<String, Object>) dataSnapshot.child(user.getUid()).child("Rezepte").getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(RezepteActivity.this, "Daten konnten nicht gelesen werden", Toast.LENGTH_SHORT).show();
}
});
}
private void fillList(Map<String, Object> entries) {
// loop to cycle through every child of "Rezepte" and get the names
for (Map.Entry<String, Object> entry : entries.entrySet()) {
Map singleEntry = (Map) entry.getValue();
// add the name to an ArrayList of all names
Rezepte.add((String) singleEntry.get("name"));
}
// put the data into the ListView
lvRezepte.addFooterView(lvRezepte, Rezepte, true);
}
应用程序总是崩溃,我在调试器中不太了解。但是错误已经发生在onDataChange void。
如果有人可以帮助我,我会非常高兴。
【问题讨论】:
标签: android firebase firebase-realtime-database