To solve this problem, move the declaration of your final int[] ndomande = {14};
array inside the onDataChange() method
, otherwise it will be always null.
public void onDataChange(DataSnapshot dataSnapshot) {
//Toast.makeText(PlayTest.this, "FIN QUI ",Toast.LENGTH_SHORT).show();
final int[] ndomande = {14};
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
//Toast.makeText(PlayTest.this, "QUI ",Toast.LENGTH_SHORT).show();
long numero_domande = (long) messageSnapshot.getValue();
ndomande[0] = (int) numero_domande;
Toast.makeText(PlayTest.this, "quante domande ci sono totali: "+ numero_domande,Toast.LENGTH_SHORT).show();
}
}
This is happening due the asynchronous behaviour of onDataChange() method
, which is called before even you are trying to get the value from that array. Moving the declaration inside the method and using it also there, solves your problem.
If you want to use the value of that array outside that method, please take a look at my answer from this post, How To Get Async Value Outside onDataChange() Method.
0
solved Firebase Query help me