0

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Pengo
  • 1
  • 1

1 Answers1

1

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

Erwin Kurniawan A
  • 958
  • 2
  • 9
  • 17
  • I tried this solution before. in here we can use this only inside getFaculty(new CallbackFaculty). I want to use it everywhere in that activity. Do you know how to use it everywhere in program. – Pengo May 29 '20 at 05:25