【问题标题】:Get Hashmap Values from realtime database从实时数据库中获取 Hashmap 值
【发布时间】:2019-12-12 16:07:45
【问题描述】:

我在获取数据快照值时遇到问题,我相信这是因为我想要访问 Hashmap<String,Hashmap>() 值。有没有办法让我获取所有数据快照并将其放入我的应用程序中的一个类中?

这是示例错误输出:

 W/System.err: com.google.firebase.database.DatabaseException:
 Class java.util.HashMap has generic type parameters, please use GenericTypeIndicator instead

示例代码:

RootRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                try {
                    if(dataSnapshot.exists()){
                        GenericTypeIndicator<Users> u = new GenericTypeIndicator<Users>(){};
                        Users usersData = dataSnapshot.getValue(u);
            }
       }
   }
}

这是实时数据库的截图。

这是我的用户类:

public class Users {
    private String id, name, phone, email,points, upline;
    private HashMap<String, String> downlines, orderHistory;
    private HashMap<String, HashMap> transactions;

    public Users(){

    }

    public Users(String id, String name, String phone, String email, String points, HashMap orderHistory, String upline, HashMap downlines, HashMap transactions) {
        this.id = id;
        this.name = name;
        this.phone = phone;
        this.email = email;
        this.points =points;
        this.orderHistory = orderHistory;
        this.downlines = downlines;
        this.upline = upline;
        this.transactions = transactions;
    }

错误表明我在Users usersData = dataSnapshot.getValue(u); 上有问题

【问题讨论】:

标签: android firebase firebase-realtime-database


【解决方案1】:

我已将 Users 类中的私有 Hashmap&lt;String, Hashmap&gt; transactions 更改为 Hashmap&lt;String, Object&gt; transactions 并且效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2021-04-25
    • 2021-10-29
    相关资源
    最近更新 更多