java - 我在 addValueEventListener 中的 dataSnapshot 中遇到错误
问题描述
我的代码中有这个事件监听器。我想获取firebase中的值。但是我的变量'ds'在我的 . getValue()
方法。`
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void showData(DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren());
UserInformation uInfo = new UserInformation();
uInfo.setAddress(ds.child(userID).getValue(UserInformation.class).getAddress());
uInfo.setDate(ds.child(userID).getValue(UserInformation.class).getDate());
uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName());
uInfo.setPhone(ds.child(userID).getValue(UserInformation.class).getPhone());
uInfo.setQuantity(ds.child(userID).getValue(UserInformation.class).getQuantity());
Log.d(TAG, "showData: address" + uInfo.getAddress());
Log.d(TAG, "showData: date" + uInfo.getDate());
Log.d(TAG, "showData: name" + uInfo.getName());
Log.d(TAG, "showData: phone" + uInfo.getPhone());
Log.d(TAG, "showData: quantity" + uInfo.getQuantity());
` 我收到错误“找不到符号 ds”
解决方案
你正在立即取消你的 for 循环:
for (DataSnapshot ds: dataSnapshot.getChildren());
您需要为您的 for 循环提供一个代码块,例如:
for (DataSnapshot ds: dataSnapshot.getChildren()) {
// do stuff with ds here
}
推荐阅读
- solidity - 检查映射中是否存在键
- node.js - Nodejs访问本地网络上的共享文件夹,根目录问题
- django - 解决django模板不存在的问题
- vagrant - 如何指定在 Vagrant 中下载到 Virtualbox VM 的 salt 版本?
- java - java for循环性能差异
- python - np.angle 返回的相位不准确
- angular - 如何正确配置代理以在 Angular/Spring Boot 中发出发布请求
- python - 内存泄漏的生成器
- google-colaboratory - 如何在 Google Colab 中将 cuDNN 从 v8.0 降级到 v6.0?
- javascript - 继续订阅一系列主题