Android Firebase数据库查询返回null [英] Android Firebase Database query returning null

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

问题描述

我试图查询Firebase数据库,但仍然返回null。这里是数据库的样子:





和Stem对象:

pre $ public class Stem {
private String stem;

public Stem(){}

public Stem(String stem){
this.stem = stem;
}

public String getText(){
return stem;
}

public void setText(String stem){
this.stem = stem;




和OnCreate中的MainActivity

  mFirebaseDatabase = FirebaseDatabase.getInstance(); 
mStemDatabaseReference = mFirebaseDatabase.getReference()。child(Stems);

getStemButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){

if(mChildEventListener == null){
mChildEventListener = new ChildEventListener(){
@Override $ b $ public void onChildAdded(DataSnapshot dataSnapshot,String s){
Stem stem = dataSnapshot.getValue(Stem.class) ;
mStemAdapter.add(stem);
}
$ b @覆盖
public void onChildChanged(DataSnapshot dataSnapshot,String s){

}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot){

}

@Override
public void onChildMoved(DataSnapshot dataSnapshot,String s){

}

@Override
public void onCancelled(DatabaseError databaseError){

}
};
mStemDatabaseReference.addChildEventListener(mChildEventListener);
}

}
});

这个查询似乎很好地访问数据库,因为它返回了Stems的json数组,但是当它试图将stem添加到它返回干的适配器为null。任何想法?

解决方案

这是由于Firebase使用异步调用来访问您的数据库。由于没有OnComplete监听器,因此应该通知适配器列表中的更改。当我遇到这个问题时,我正在使用RecyclerView,并在适配器上调用 notifyDataSetChanged 来使用提取的值更新它。


I'm trying to query a Firebase Database but keep getting null returned. Here is what the database looks like:

And the Stem object:

public class Stem {
    private String stem;

    public Stem(){}

    public Stem(String stem){
        this.stem = stem;
    }

    public String getText(){
        return stem;
    }

    public void setText(String stem){
        this.stem = stem;
    }
}

And the MainActivity in the OnCreate

mFirebaseDatabase = FirebaseDatabase.getInstance();
mStemDatabaseReference = mFirebaseDatabase.getReference().child("Stems");

 getStemButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

if (mChildEventListener == null) {
                    mChildEventListener = new ChildEventListener() {
                        @Override
                        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                            Stem stem = dataSnapshot.getValue(Stem.class);
                            mStemAdapter.add(stem);
                        }

                        @Override
                        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                        }

                        @Override
                        public void onChildRemoved(DataSnapshot dataSnapshot) {

                        }

                        @Override
                        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    };
                    mStemDatabaseReference.addChildEventListener(mChildEventListener);
                }

            }
        });

The query seems to access the database fine because it returns the Stems json array, but when it tries to add the stem to the adapter it returns stem as null. Any ideas?

解决方案

This is due to Firebase using asynchronous calls to access your database. As there is no OnComplete listener for this, you should notify your adapter for changes in the list. I was using RecyclerView when I faced this and just called notifyDataSetChanged on the adapter to update it with the fetched values.

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

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