With this code I'm supposed to be able to retrieve a value directly from a RTDB (realtime database) and show it in the screen of my app, but nothing happens, app compiles fine. I hope you could help me.
On the image lays the simple structure of my RTDB
public class MainActivity extends AppCompatActivity {
private static final String PATH_START = "start";
private static final String PATH_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tvMessage = findViewById(R.id.tvMessage);
FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference reference = database.getReference(PATH_START)
.child(PATH_MESSAGE);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
tvMessage.setText(snapshot.getValue(String.class));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(MainActivity.this, "not able to read in Firebase",
Toast.LENGTH_LONG).show();
}
});
}
}