从实时数据库获取哈希图值

时间:2019-08-05 06:45:09

标签: android firebase firebase-realtime-database

我在获取数据快照值时遇到问题,我相信它是因为我想访问一个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);
            }
       }
   }
}

这是实时数据库的屏幕截图。

Firebase Realtime Database

这是我的用户类别:

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);上有问题

1 个答案:

答案 0 :(得分:0)

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