0

When the user starts the activity, a Toast should pop up containing parent node where the value "drinkManufacturerID" is equal to a given String. However, a null value is being returned in the Toast.

Relevant code:

public class DealRawDataActivity extends AppCompatActivity {

DatabaseReference databaseDrinks;

String keys;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_deal_raw_data);

    databaseDrinks = FirebaseDatabase.getInstance().getReference("drinks").child("-LWLuM2nesg0uaP0dLSn");

    databaseDrinks.orderByChild("drinkManufacturerID").equalTo("D1eY5v9guwSIWMvfLoq8xGywrT53").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot datas : dataSnapshot.getChildren()) {
                keys = datas.getKey();
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

   Toast.makeText(this, keys, Toast.LENGTH_LONG).show();
}

}

Firebase Realtime Database Structure: enter image description here

JustReflektor
  • 95
  • 3
  • 13

1 Answers1

1

When trying to display a Toast message containg the value of your keys variable outside the callback, you'll always get null since the onDataChange() has an asynchronous behavior. Because that method returns immediately, the value of your keys variable you're trying to display, has not been populated from the callback yet. There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available.

A quick solve for this problem would be to use move the following line of code:

Toast.makeText(this, keys, Toast.LENGTH_LONG).show();

Right after the for loops ends, otherwise I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

Edit: Please see below the entire code:

public class DealRawDataActivity extends AppCompatActivity {
    DatabaseReference databaseDrinks;
    String keys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_deal_raw_data);
        databaseDrinks = FirebaseDatabase.getInstance().getReference("drinks").child("-LWLuM2nesg0uaP0dLSn");
        databaseDrinks.orderByChild("drinkManufacturerID").equalTo("D1eY5v9guwSIWMvfLoq8xGywrT53").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot datas : dataSnapshot.getChildren()) {
                    keys = datas.getKey();
                }
                Toast.makeText(this, keys, Toast.LENGTH_LONG).show(); //Should work!!
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193