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.