I am reading some data from Firebase's real-time database. I want to read the data and then display it in a recycleView. When I enter debug mode it is reading the data and saving it to a local variable, but for some reason, it is returning null when it is being call by the recycleView adapter. I have some data that I am also writing as well and that is working, so I know the firebase database is connecting.
My Homeblinds class (where the connection to firebase is made and receiving the data)
public class HomeBlinds {
DatabaseReference ref;
String temp,light,blindkey,location;
// this will house data on the specific blind.
public HomeBlinds(String test,String blindkey) {
this.blindkey = blindkey;
ref = FirebaseDatabase.getInstance().getReference(blindkey);
}
public String getLocation() {
ref.child("Location").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
String l = String.valueOf(snapshot.getValue());
location = l;
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
return location;
}
My recycleView adapter class
@Override
public void onBindViewHolder(@NonNull HomeViewHolder holder, int position) {
HomeBlinds x = testblinds.get(position);
String location = x.getLocation();
holder.loc.setText("Location: "+String.valueOf(x.getLocation()));
holder.temp.setText("Temperature: "+String.valueOf(x.getTemperature()));
holder.light.setText("Light: "+String.valueOf(x.getLight()));
Photo of the Firebase real-time database
Current output