Firebase查询对我有帮助 [英] Firebase Query help me

查看:58
本文介绍了Firebase查询对我有帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

final int[] ndomande = {14};

//Database
mFirebaseInstance = FirebaseDatabase.getInstance();

final DatabaseReference Refndom = mFirebaseInstance.getReference("Dati");
final Query queryn = Refndom.orderByChild("numero_domande");

queryn.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        //Toast.makeText(PlayTest.this, "FIN QUI ",Toast.LENGTH_SHORT).show();

        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();
        }

    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(PlayTest.this, "???", Toast.LENGTH_SHORT).show();
    }
});

//PROBLEM ON NDOMANDE 

Toast.makeText(PlayTest.this, "x: "+ ndomande[0] ,Toast.LENGTH_SHORT).show();
Random random = new Random();
final int x = random.nextInt(ndomande[0]);
 Toast.makeText(PlayTest.this, "x: "+ndomande[0] ,Toast.LENGTH_SHORT).show();

数组不返回查询的值,但是我为初始化设置的值将帮助我进行异步阻塞查询,有人可以帮助我吗?

the array does not return the value of the query but the value I set for initialization would help me make an asynchronous blocking query someone can help me?

推荐答案

要解决此问题,请将final int[] ndomande = {14};数组的声明移到onDataChange() method内,否则它将始终为null.

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();
    }
}

这是由于onDataChange() method的异步行为而发生的,即使您试图从该数组中获取值,该行为也会被调用.将声明移到方法内部并在那里使用它可以解决您的问题.

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.

这篇关于Firebase查询对我有帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆