如何强制onDataChange异步函数完成并完成其他代码

时间:2019-03-12 09:43:51

标签: android firebase real-time

我正在使用android应用程序,我想从数据库中检索所有医院的电子邮件,并将它们保存在arraylist中,然后检查列表中是否存在特定的医院电子邮件,但是问题是在运行时..尚未完成检索所有信息,然后搜索有关该电子邮件的信息,而不是进行搜索。它开始进行检索,并在完成检索过程之前继续执行其余代码。因此,问题是我如何通过将程序保存在arrylist中然后使用该arraylist来检查是否存在特定的电子邮件来强制执行该程序以完成检索过程。.解决异步问题的其他解决方案仅适用于打印数据从firebase,但是在我的设置中,我想检索arrayList中的所有数据,然后使用它们。我该怎么做?因为我在这里阅读了3页,但是找不到适合我的情况的解决方案。
这是用于搜索和检索的代码

(!db.retrieve_All_Hospitals().isEmpty()){
            List<hospital> hospitals = db.retrieve_All_Hospitals();
            for (int i=0; i<hospitals.size(); i++){
                hospital h = hospitals.get(i);
                if(h.hospital_email == hospital_email){
                    alertbox("","Email is already exist in the system");
                    hospital_email_et.setError("Email is already exist");
                    return;
                }

这是用于检索电子邮件的功能。

//retrieve all hospitals
public List<hospital> retrieve_All_Hospitals(){
    List<hospital> hospital_List = new ArrayList<>();
    ref = db.getReference(hospital_Table);
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Log.d("in firebase","working");
            for(DataSnapshot ds : dataSnapshot.getChildren()) {
                String code = ds.getKey();

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

0 个答案:

没有答案