从Firebase数据库检索到的值在valueEventListener之外的使用

huangapple go评论83阅读模式
英文:

Use value retrieved from Firebase database outside of valueEventListener

问题

我正在使用Java和Firebase在Android中开发一个应用程序,在其中我从Firebase数据库中检索数据。以下是我的代码:

databaseReference.orderByChild("email").equalTo(email).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    AddFaculty addFaculty = dataSnapshot1.getValue(AddFaculty.class);
                    department = addFaculty.getDepartment();
                    year = addFaculty.getYear();
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

模型类

public class AddFaculty {
    private String name;
    private String email;
    private String position;
    private String department;
    private String year;
    private String facultyId;

    public AddFaculty() {
    }

    public AddFaculty(String name, String email, String position, String department, String year, String facultyId) {
        this.name = name;
        this.email = email;
        this.position = position;
        this.department = department;
        this.year = year;
        this.facultyId = facultyId;
    }

    // 省略其他getter和setter方法
}

这是我的问题。我想在valueEventListener之外使用'addFaculty'、'department'和'year'变量。我知道很多人问过与此相关的问题。我尝试了许多与此问题相关的解决方案,但都没有成功。所以如果你知道如何实现这一点,那将是很大的帮助。

英文:

I'm developing an app using Java and Firebase in android in that I an retrieving data from Firebase Database. Here is my code

databaseReference.orderByChild("email").equalTo(email).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    AddFaculty addFaculty = dataSnapshot1.getValue(AddFaculty.class);
                    department = addFaculty.getDepartment();
                    year = addFaculty.getYear();
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

Model class

public class AddFaculty {
    private String name;
    private String email;
    private String position;
    private String department;
    private String year;
    private String facultyId;

    public AddFaculty() {
    }

    public AddFaculty(String name, String email, String position, String department, String year, String facultyId) {
        this.name = name;
        this.email = email;
        this.position = position;
        this.department = department;
        this.year = year;
        this.facultyId = facultyId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getFacultyId() {
        return facultyId;
    }

    public void setFacultyId(String facultyId) {
        this.facultyId = facultyId;
    }
}

Here is my problem. I want to use 'addFaculty' or department and year variable outside of the valueEventListener. I know many people asked question related to this. I tried many solutions related to this question but it didn't work.
So if you know how to achieve this it would be great help.

答案1

得分: 1

创建一个访问变量的接口:

interface CallbackFaculty {
    void passFaculty(AddFaculty addFaculty);
}

然后您可以创建一个带有接口参数的函数:

private void getFaculty(CallbackFaculty callbackFaculty) { 
     databaseReference.orderByChild("email").equalTo(email).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                AddFaculty addFaculty = dataSnapshot1.getValue(AddFaculty.class);
                department = addFaculty.getDepartment();
                year = addFaculty.getYear();
                callbackFaculty.passFaculty(addFaculty);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

使用该方法:

getFaculty(new CallbackFaculty {
    @Override
    public void passFaculty(AddFaculty addFaculty) {
         // 在这里您可以访问addFaculty变量
         // 您可以尝试将addFaculty记录下来以检查其值
    }
});

希望对您有所帮助。

英文:

Create an interface to access the variable

interface CallbackFaculty {
    void passFaculty(AddFaculty addFaculty);
}

then you can create a function with the interface parameter

private void getFaculty(CallbackFaculty callbackFaculty) { 
     databaseReference.orderByChild("email").equalTo(email).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                AddFaculty addFaculty = dataSnapshot1.getValue(AddFaculty.class);
                department = addFaculty.getDepartment();
                year = addFaculty.getYear();
                callbackFaculty.passFaculty(addFaculty);
            }

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

to use the method

getFaculty(new CallbackFaculty {
    @Override
    public void passFaculty(AddFaculty addFaculty) {
         // here you have the access of addFaculty
         // you can try logging addFaculty to check the value
    }
});

hope it helps you

huangapple
  • 本文由 发表于 2020年5月29日 11:35:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/62078263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定