英文:
Failed to convert value of type java.util.HashMap to String. Converting string to map
问题
以下是我的代码。我遇到了这个错误:“com.google.firebase.database.DatabaseException: 无法将类型为java.util.HashMap的值转换为String”。请帮忙。
du = FirebaseDatabase.getInstance().getReference().child("storename");
du.keepSynced(true);
du.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
};
Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
name.setText(map.get("ownerName"));
storename.setText(map.get("compName"));
storeaddr.setText(map.get("address"));
ph1.setText("Ph: " + map.get("phoneNumber"));
gst.setText(map.get("gstNum"));
}
@Override
public void onCancelled(DatabaseError error) {
}
});
英文:
Below is my code. I get this error "com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String" . Please help.
du = FirebaseDatabase.getInstance().getReference().child("storename");
du.keepSynced(true);
du.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
};
Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
name.setText(map.get("ownerName"));
storename.setText(map.get("compName"));
storeaddr.setText(map.get("address"));
ph1.setText("Ph: " + map.get("phoneNumber"));
gst.setText(map.get("gstNum"));
}
@Override
public void onCancelled(DatabaseError error) {
}
});
答案1
得分: 0
你正在从 /storename
加载所有数据。由于可能有多个商店在其中,你需要循环遍历 dataSnapshot.getChildren()
来访问每个独立的商店。
因此:
du = FirebaseDatabase.getInstance().getReference().child("storename");
du.keepSynced(true);
du.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
};
Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
name.setText(map.get("ownerName"));
storename.setText(map.get("compName"));
storeaddr.setText(map.get("address"));
ph1.setText("Ph: " + map.get("phoneNumber"));
gst.setText(map.get("gstNum"));
}
}
@Override
public void onCancelled(DatabaseError error) {
throw error.toException(); // 永远不要忽视错误
}
});
英文:
You're loading all data from /storename
. Since there may be multiple stores under there, you'll need to loop over dataSnapshot.getChildren()
to get to each individual store.
So:
du = FirebaseDatabase.getInstance().getReference().child("storename");
du.keepSynced(true);
du.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot: snapshot.getChildren()) {
GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
};
Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
name.setText(map.get("ownerName"));
storename.setText(map.get("compName"));
storeaddr.setText(map.get("address"));
ph1.setText("Ph: " + map.get("phoneNumber"));
gst.setText(map.get("gstNum"));
}
}
@Override
public void onCancelled(DatabaseError error) {
throw error.toException(); // never ignore errors
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论