[英]Android retrieve the data from Firebase and save it in ArrayList
我在 firebase 中有一个评级列表。 评分列表的子节点是用户名,然后是地名和评分。 我想将地名和评级数据保存在不同的数组列表中。
问题:
地名数组 ['Bricks Diner', 'Kingstreet Café', 'Royce Hotel'], [AV Rani Supermart Sdn Bhd, FamilyMart Plaza Sentral..]
评级数组 [3, 1.5, 3.5], [5, 1.5]...
为此,请使用以下代码行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference ratingRef = rootRef.child("Rating");
ratingRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
@Override
public void onComplete(@NonNull Task<DataSnapshot> task) {
if (task.isSuccessful()) {
for (DataSnapshot userSnapshot : task.getResult().getChildren()) {
List<String> places = new ArrayList<>();
List<Double> ratings = new ArrayList<>();
for (DataSnapshot placeSnapshot : userSnapshot.getChildren()) {
places.add(placeSnapshot.getKey());
ratings.add(placeSnapshot.child("rating").getValue(Double.class));
}
Log.d("TAG", places.toString());
Log.d("TAG", ratings.toString());
}
} else {
Log.d(TAG, task.getException().getMessage()); //Don't ignore potential errors!
}
}
});
第一条日志语句将打印在 logcat 中:
[Bricks Diner, Kingstreet Café, Royce Hotel]
[AV Rani Supermart Sdn Bhd, FamilyMart Plaza Sentral]
...
第二条日志语句将打印在 logcat 中:
[3, 1.5, 3.5]
[5, 1.5]
...
注意事项:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.