2

I can't figure out how to retrieve my data without variable under the push key, so please help.

    reference = FirebaseDatabase.getInstance().getReference("FireAlarm");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot keySnapshot : dataSnapshot.getChildren()){
                dataBaru = keySnapshot.getKey();
                data.setText(dataBaru);
            }
        }

Database Structure

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Izz
  • 77
  • 11

1 Answers1

2

When you are using the following line of code:

dataBaru = keySnapshot.getKey();

You are getting only those pushed keys. If you want to get the values corresponding to the keys, please use the following line of code:

dataBaru = keySnapshot.getValue(String.class);

One more thing, when looping through a DataSnapshot object using getChildren() method, you're most likely getting more than one result so you can simply set it to a TextView. So probably you should use a ListView for that or even better a RecyclerView.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • thanks for the help , it works , planning on using recyclerview instead :) – Izz Mar 26 '19 at 10:21
  • For a `RecyclerView`, **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase realtime database using `FirebaseRecyclerAdapter`. – Alex Mamo Mar 26 '19 at 10:22