ArrayList不会存储Firebase数据库列表

时间:2018-04-10 15:26:55

标签: android firebase arraylist firebase-realtime-database

我想将我的数据库子存储在我的模型的数组列表中。

型号:

public class testInformation {
private String taskName;
private String answer;

public testInformation() {
}

public String getTaskName() {
    return taskName;
}

public void setTaskName(String taskName) {
    this.taskName = taskName;
}

public String getAnswer() {
    return answer;
}

public void setAnswer(String answer) {
    this.answer = answer;
}
}

在片段的onCreate()方法中,我调用GetDataFromDatabase()方法,如下所示:

public void GetDataFromDatabase(){
    mFirebaseDatabase= FirebaseDatabase.getInstance();
    myref=mFirebaseDatabase.getReference().child("solutionKey");
    myref.keepSynced(true);
    myref.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds:dataSnapshot.getChildren()){
                testInformation testInformation=new testInformation();
                testInformation.setTaskName(ds.getKey().toString());
                testInformation.setAnswer(ds.getValue().toString());
                results.add(testInformation);
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

我有一个名为ArrayList<testInformation>的公共results变量,用于存储数据库中的数据,但即使在调用GetDataFromDatabase()之后,results列表也是如此似乎是空的,但是当我在OnDataChange()方法上设置断点时,它似乎充满了正确的数据。

我的问题是,如何从results获取填好的OnDataChange()列表来使用它?

修改:此列表不会被修改或更改,因此我只需要获取一次,这就是我使用addListenerForSingleValueEvent()方法的原因。

2 个答案:

答案 0 :(得分:0)

这就是代码的结构:

public class EndOfTestFragment extends Fragment {


    private FirebaseDatabase mFirebaseDatabase;
    private DatabaseReference myref;
public ArrayList<testInformation> results;

...

@Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
...
GetDataFromDatabase();
...
}
public void GetDataFromDatabase(){
        mFirebaseDatabase= FirebaseDatabase.getInstance();
        myref=mFirebaseDatabase.getReference().child("solutionKey");
        myref.keepSynced(true);
        myref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds:dataSnapshot.getChildren()){
                    testInformation testInformation=new testInformation();
                    testInformation.setTaskName(ds.getKey().toString());
                    testInformation.setAnswer(ds.getValue().toString());
                    results.add(testInformation);
                }
            }
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

答案 1 :(得分:0)

public class EndOfTestFragment extends Fragment {
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference myref;
public ArrayList<testInformation> results;

//...

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    //...
    GetDataFromDatabase();// This returns before results.add(...) is called
    // Whatever code is after this line gets run before results list has data
    /* LABEL#1 */
    // The results list is probably NOT populated yet so don't use it here
}
public void GetDataFromDatabase() {
    mFirebaseDatabase= FirebaseDatabase.getInstance();
    myref=mFirebaseDatabase.getReference().child("solutionKey");
    myref.keepSynced(true);

    myref.addListenerForSingleValueEvent(new ValueEventListener() {// This function returns immediately, does not wait for data from Firebase...
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            /* LABEL#2 */
            for (DataSnapshot ds:dataSnapshot.getChildren()){
                testInformation testInformation=new testInformation();
                testInformation.setTaskName(ds.getKey().toString());
                testInformation.setAnswer(ds.getValue().toString());
                results.add(testInformation);
            }
            // The results list is populated now, do whatever you want with it here
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

LABEL中的代码# 1 执行之前#LABEL# 2

只要数据可用,就会在后台调用onDataChange(...)函数。
addListenerForSingleValueEvent()不等待数据并立即返回。