tried retrieve data from firebase when user click the button, but the data not displayed if the button is click for the first time, then if i click again the data is displayed.. any idea what i should adding to the code so the data is appear on the first click attempt..
here are the code :
btnprediksi.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
efekGK();
}
});
here is the efekGK method
private void efekGK() {
final DatabaseReference CekDataGK;
posisi="GK";
CekDataGK=FirebaseDatabase.getInstance().getReference();
CekDataGK.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child("PlayerSkills").child(username).child("GK").exists()) {
PlayersSkill playerData = dataSnapshot.child("PlayerSkills")
.child(username)
.child("GK")
.getValue(PlayersSkill.class);
assert playerData != null;
String basGKform = playerData.getNumform();
String basGKExp=playerData.getNumexperience();
Toast.makeText(HomeActivity.this, basGKform, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
i using Toast to check if data is available or not. the result is when i click button toast displayed nothing, but when i click again Toast displaying the data I expected...
EDIT : I follow Alexios suggestion and it's works perfectly if the variable display inside the "efekgk" method.. but if I put it outside the method it's display nothing again at first click.
first I define the variable outside the method
public class HomeActivity extends AppCompatActivity {
private String basGKform, basGKExp;
.....}
and i moved the result into new method called displaydata
private void displaydata() {
Toast.makeText(HomeActivity.this, basGKform, Toast.LENGTH_SHORT).show();
}
Resulting none again in its first click