无法以列表

时间:2016-11-03 10:13:02

标签: android firebase firebase-realtime-database

firebase中的数据如下所示

data image here

我希望将数据标记为userId作为列表或数组。

我的方法是:

public static void writePostToFriends(final String userId, final String userName, final String title, final String body, final String authorPhoto){
    DatabaseReference mpostRef = FirebaseDatabase.getInstance().getReference().child("social/user/"+userId+"/friends");
    mpostRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            GenericTypeIndicator<List<Friends>> t = new GenericTypeIndicator<List<Friends>>() {};
            List<Friends> mFriends = dataSnapshot.getValue(t);
            long j = mFriends.size();
            for (long i = 0; i == j; i++){
                Friends mReadFriends = dataSnapshot.getValue(Friends.class);
                String getUid = mReadFriends.userID;
                writeNewPost(getUid, userId, userName, title, body, authorPhoto);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

但是当它尝试阅读时会出现此错误 -

  

过程:com.evolutionstudios.atmiyo,PID:19506                                                                                com.google.firebase.database.DatabaseException:在反序列化时期望一个List,但得到一个类java.util.HashMap                                                                                    在com.google.android.gms.internal.zzanp.zza(未知来源)                                                                                    在com.google.android.gms.internal.zzanp.zza(未知来源)                                                                                    在com.google.android.gms.internal.zzanp.zza(未知来源)                                                                                    在com.google.firebase.database.DataSnapshot.getValue(未知来源)                                                                                    at com.evolutionstudios.atmiyo.Utils.NotificationHelper $ 1.onDataChange(NotificationHelper.java:40)                                                                                    在com.google.android.gms.internal.zzakg.zza(未知来源)                                                                                    在com.google.android.gms.internal.zzalg.zzcxk(未知来源)                                                                                    在com.google.android.gms.internal.zzalj $ 1.run(未知来源)                                                                                    在android.os.Handler.handleCallback(Handler.java:739)                                                                                    在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                    在android.os.Looper.loop(Looper.java:148)

任何人都可以帮我弄清楚如何将数据中的userID作为数组或列表获取吗?

1 个答案:

答案 0 :(得分:24)

错误消息包含它的要点:

  

在反序列化时期望一个List,但得到一个类java.util.HashMap

如果查看数据库,则会看到一组键+值。在Java中,转换为List而不是Map

如果您只对这些值感兴趣,则需要在代码中处理从Listpublic void onDataChange(DataSnapshot dataSnapshot) { GenericTypeIndicator<Map<String, Friends>> t = new GenericTypeIndicator<Map<String, Friends>>() {}; Map<String, Friends> map = dataSnapshot.getValue(t); // get the values from map.values(); 的转换:

public void onDataChange(DataSnapshot dataSnapshot) {
    List<Friends> list = new ArrayList<Friends>();
    for (DataSnapshot child: dataSnapshot.getChildren()) {
        list.add(child.getValue(Friends.class));
    }
}

或更简单:

moment.js