将数据列表推送到firebase无法正常工作

时间:2016-08-30 15:15:15

标签: 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

1 个答案:

答案 0 :(得分:1)

保存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()只是一种方法。如果您想在FirebaseListAdapterFirebaseRecyclerAdapter中显示答案,也可以使用ListViewRecyclerView

相关问题