Reputation: 49
I have String values in firebase database I would like to get this String in onClick i make a toast for display the value but the value is toasted in the second click
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_souslistecuisine);
cuisinecomplet=(ImageView)findViewById(R.id.cuisinecomplett);
FirebaseDatabase database= FirebaseDatabase.getInstance();
DatabaseReference myRef=database.getReference("Produit");
DatabaseReference key=myRef.child("Cuisine");
DatabaseReference text=key.child("cuisine complet");
text1=text.child("text1");
text1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
txt=dataSnapshot.getValue(String.class);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public void complet(View view) {
Toast.makeText(souslistecuisine.this, txt, Toast.LENGTH_LONG).show();
}
I tried to put text1.addValueEventListener(new ValueEventListener())
in Onclick
method but same result
Upvotes: 0
Views: 63
Reputation: 40820
Just add the Toast
after you already receive the String value from Firebase
, as Firebase
callbacks run in background thread and take time to response.
text1.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
txt=dataSnapshot.getValue(String.class);
Toast.makeText(souslistecuisine.this, txt, Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
Upvotes: 1