简体   繁体   English

如何读取 Firebase 实时数据库中的嵌套数据(哈希图)

[英]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.

相关问题 我如何在需要时而不只是在 DataChange 时从 firebase 实时数据库中读取数据 - how can i read data from firebase realtime database when i want and not just when DataChange 如何从 Firebase 实时数据库读取数据 - How to read data from the Firebase realtime database Firebase 实时数据库 Hashmap 覆盖数据 - Firebase realtime Database Hashmap overwrites data 如何通过 POJO 类从 Firebase 实时数据库中读取嵌套父节点的值? - How do I read values from a firebase realtime database for nested parent nodes via POJO classes? 如何检查用户是否已在firebase身份验证和firebase实时数据库中的数据之间注册和匹配数据? - How can I check user had registered and match data between data on firebase authentication and firebase Realtime Database? 如何使用 AndroidStudio 中的 Java 方法从 Firebase 实时数据库读取值? - How can I read values from Firebase Realtime Database by using a Java method in AndroidStudio? Firebase 实时数据库 - 读取嵌套数据 - Firebase Realtime Database - reading nested data 从Firebase实时数据库读取数据 - Read data from firebase realtime database 无法在 Firebase 实时数据库上读写 - can not read or write on Firebase Realtime database 如何将数据从实时 firebase 数据库中提取到数组中? 我希望将数组中的静态数据更改为 firebase 数据 - How can I pull data from realtime firebase database into an array? I wish to change static data in an array to firebase data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM