android - 将数据列表推送到 firebase 不起作用

标签 android database firebase push

我正在尝试使用 firebase push() 函数,因为我想将数据列表添加到现有的列表中。 setValue() 函数会覆盖现有数据。

这是我以前做的:

 DatabaseReference childref =  mDatabase.child("users").child(uih.getUserData().getUsername()).child("answered_questions");
                        childref.setValue(getAnsweredQuestions(questionViewList));

这行得通,但每次我使用此功能时,数据都会被覆盖,这不是我想要的。我尝试使用 firebase 文档中描述的 Push 函数:https://firebase.google.com/docs/database/android/save-data

我不确定我做的对不对,但它不起作用。这是我尝试实现 push() 函数的时候:

DatabaseReference childref =  mDatabase.child("users").child(uih.getUserData().getUsername()).child("answered_questions");
                    String key = childref.push().getKey();    
                    Map<String, Object> childUpdates = new HashMap<>();
                    childUpdates.put( key, questionViewList);
                    mDatabase.updateChildren(childUpdates);

我得到的异常是:

com.google.firebase.database.DatabaseException: Failed to parse node with class class scrambled.nl.generationr.QuestionView

这很奇怪,因为我在执行 setValue 方法时没有收到此错误。谁能解释我做错了什么以及我应该如何将列表推送到 firebase?

编辑:

我能做的是:

DatabaseReference childref =  mDatabase.child("users").child(uih.getUserData().getUsername()).child("answered_questions");
                            childref.push().setValue(getAnsweredQuestions(questionViewList));

在此处添加了 push()。这行得通,但我不只是增加我的列表,而是在我的列表中添加了另一层,这样我实际上得到了一个数组数组而不是更长的列表。

查看结果:

enter image description here

最佳答案

保存 AnsweredQuestion 对象列表:

这假设您在设计 AnsweredQuestion.class 时已遵循规则,以便 Java 对象可用于在 Firebase 中存储数据。如果您需要有关该检查的指导,请在“基本写入操作”标题下检查以保存文档中的数据。

//List of AnsweredQuestions
List<AnsweredQuestion> mAllAnswers;
....

//create the database reference that points to the correct parent node
//where answeres are stored for each user    
DatabaseReference ref = mDatabase.child("users").child(uih.getUserData().getUsername()).child("answered_questions");

//Iterate over your List of AnsweredQuestion objects and use push() along with setValue() 
//to store a single AnsweredQuestion object to a unique location in your database.
for(AnsweredQuestion answer : mAllAnswers){ 
    ref.push().setValue(answer);
}

检索用户所有已回答的问题:

//create List to store AnsweredQuestion object
List<AnsweredQuestion> mAllAnswers = new ArrayList<AnsweredQuestion>();
...

ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //iterate over datasnapshot to get/store each AnsweredQuestion object
            if(datSnapshot.exists()){
                for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                   AnsweredQuestion answer = snapshot.getValue(AnsweredQuestion.class);
                   mAllAnswers.add(answer);
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            //handle error
        }
}); 

有多种方法可以检索每个用户的答案,使用 .addListenerForSingleValueEvent() 只是一种方法。如果您想在 ListViewRecyclerView 中显示答案,您还可以使用 FirebaseListAdapterFirebaseRecyclerAdapter

关于android - 将数据列表推送到 firebase 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39231296/

相关文章:

java - 在 Android 后台自动发送邮件

android - 为什么可检查的菜单项不显示复选框

mysql - 我应该安装哪个驱动程序以便可以使用 powershell 运行 mysqlcommand?

java - 如何在Android Studio中连接在线数据库

Android 和 FileProvider 并从 URI 获取路径

android - Google Maps Android API v2 不显示 map

.net - Sql 连接类 类与模块 - .NET

javascript - 如何将 Firebase 中的节点(对象)复制到另一个节点?

javascript - 更新 firestore 文档中的字段

javascript - 如何在 Google Firestore 中进行分页?