[英]How can I read nested data (hashmap) in Firebase RealTime Database
I follow with this tutorial and save mutliple image to my firebase database:我遵循本教程并将多个图像保存到我的 firebase 数据库中:
Gallery is hashmap, how I Can read this data?图库是 hashmap,我怎样才能读取这些数据?
My POJO class:我的 POJO class:
public class Tasks {
String titletask, decriptiontask, datetask, keytask, imagetask;
HashMap<String,String> gallery;
When I try with this code:当我尝试使用此代码时:
mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
{
Tasks task = dataSnapshot1.getValue(Tasks.class);
list.add(task);
}
adapter = new TasksAdapter(getActivity(),list);
mRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
I have a error:我有一个错误:
Failed to convert value of type java.util.HashMap to String
无法将 java.util.HashMap 类型的值转换为字符串
This field in your Tasks
class:您的
Tasks
class 中的此字段:
HashMap<String,String> gallery;
Would translate to a JSON structure like this:将转换为 JSON 结构,如下所示:
"gallery": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
}
Since that is not the structure you have, the mapping will fail.由于这不是您拥有的结构,因此映射将失败。
To describe you JSON you'll either need to define a class for each gallery item:为了描述你 JSON 你需要为每个画廊项目定义一个 class :
public class GalleryItem {
public String ImgLink;
}
And then:接着:
HashMap<String,GalleryItem> gallery;
Alternatively you may be able to get away with Object
:或者,您也许可以摆脱
Object
:
HashMap<String,Object> gallery;
And (if this indeed works) treat the values in the gallery
map as Map<String, String>
themselves.并且(如果这确实有效)将
gallery
map 中的值视为Map<String, String>
本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.