本文介绍了无法从Firebase数据库检索数据字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去5天,我一直试图从我的firebase数据库检索数据字段,但没有取得任何成功。这是我的数据库的样子:





用于获取数据的代码:

pre $ private $ {private $ alterTextView(final String id){
if(id!= null){
mDatabase.child(Users)。child(id).addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
UserInformation userInformation = dataSnapshot.getValue(UserInformation.class);
String name =(String)userInformation.getName();

$ b Log.d(Main Activity,name);
}

@Override
public void onCancelled(DatabaseError databaseError){


});
mWelcomeUserMessage.setText(Welcome,+ name);



$ / code $ / pre

我不知道为什么名字是'空'(参考日志)。
有人可以告诉我我做错了什么?



PS:我在UserInformation.java类中定义了一个空白的默认构造函数,getters和setter 。

此外,mDatabase的初始化如下:
mDatabase = FirebaseDatabase.getInstance()。getReference();

在UserInformation类中的名称字段public。另外请确保您调用getValue的快照是UserInformation类的有效JSON表示,并具有完全相同的name字段(它必须与类中的名称字段完全匹配)。

请注意:
addValueEventListener 会添加一个侦听器,当数据可用时将会调用该侦听器。



请注意,在 onDataChange 被调用后,数据可用。这就是为什么您应该在从 DataSnapshot 获取所需数据后立即设置文本。像这样:

pre prerivate $ private static void alterTextView(final String id){
if(id!= null){
mDatabase.child(Users)。child(id).addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){$ b $ UserInformation userInformation = dataSnapshot。 getValue(UserInformation.class);
String name =(String)userInformation.getName();
Log.d(Main Activity,name);
runOnUiThread(new Runnable(){
@Override
public void run(){
mWelcomeUserMessage.setText(Welcome,+ name);
}
});
}

@Override
public void onCancelled(DatabaseError databaseError){

}
});

}
}


I have been trying to retrieve data fields from my firebase database for the past 5 days without any success. This is what my database looks like:

Code for fetching data:

private void alterTextView(final String id) {
        if(id!=null) {
        mDatabase.child("Users").child(id).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                UserInformation userInformation = dataSnapshot.getValue(UserInformation.class);
                String name = (String) userInformation.getName();


                Log.d("Main Activity",name);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        mWelcomeUserMessage.setText("Welcome, "+name);

    }
}`

I have no idea why the name is 'null' (refer to log).Can someone tell me what I'm doing wrong?

PS: I have defined a blank default constructor, getters, and setters in my UserInformation.java class.

Also, mDatabase is initialized as follows:mDatabase = FirebaseDatabase.getInstance().getReference();

解决方案

dataSnapshot.getValue(Class) will only load and set values into public fields. I see you already have a default constructor.

Make name field in UserInformation class public. Also make sure the snapshot you are calling getValue on is a valid JSON representation of UserInformation class and has an exact same "name" field (it must exactly match the name field in class).

Please note:addValueEventListener adds a listener, which will be called when data is available.

Please note that the data is available after onDataChange has been called. That's why you should set your text right after you get the data you need from the DataSnapshot. Like that:

private void alterTextView(final String id) {
            if(id!=null) {
            mDatabase.child("Users").child(id).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    UserInformation userInformation = dataSnapshot.getValue(UserInformation.class);
                    String name = (String) userInformation.getName();
                    Log.d("Main Activity",name);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run(){
                            mWelcomeUserMessage.setText("Welcome, "+name);
                        }
                    });
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    }

这篇关于无法从Firebase数据库检索数据字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 18:39