android - 如何检索 firebase 数据库条目的细节?

标签 android firebase firebase-realtime-database

我一直在尝试从我存储在 firebase 实时数据库中的数据中检索数据字段,但我被卡住了。

这是我的数据库的样子: firebase realtime database

这是我在 AuthStateListener 对象中调用的方法,定义如下:

private void alterTextView(final String id) {
    if(id!=null) {
        mDatabase.child("Users").child(id).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    if (ds.exists()) {
                        UserInformation userInformation = ds.child(id).getValue(UserInformation.class);
                        String name = userInformation.getName();
                        mWelcomeUserMessage.setText(name);
                        //logging name
                        Log.d("My activity", "name of user with userID - " + id + " is : " + userInformation.getName());
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

这个函数的调用是这样的:

 mAuthStatelistener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            //if user is logged in, the sign_in and sign_up buttons should not be displayed
            if (firebaseAuth.getCurrentUser() != null) {
                userId = firebaseAuth.getCurrentUser().getUid();
                Log.d("Main Activity", "Current userId : " + userId);
                alterTextView(userId);
                mLogInButton.setVisibility(View.INVISIBLE);
                mSignUpButton.setVisibility(View.INVISIBLE);
                //getUserName(firebaseAuth.getCurrentUser().getUid());
                mLogOutButton.setVisibility(View.VISIBLE);
            } else {
                mLogInButton.setVisibility(View.VISIBLE);
                mSignUpButton.setVisibility(View.VISIBLE);
                mWelcomeUserMessage.setVisibility(View.GONE);
                mLogOutButton.setVisibility(View.GONE);
            }
        }
    };

出于某种原因,我在存储 userInformation 对象名称的行的第一个代码摘录中不断收到空指针异常 -

String name = userInformation.getName();

我只是不明白为什么会这样。

我的主要目标是在他/她登录后打印出登录用户的姓名。

有谁知道这样做的方法吗?

更新 1 这是我的 UserInformation 类的样子:

public class UserInformation {

private String name;
private String student_number;
private String faculty;
private boolean isAdmin;

public String getName() {
    return name;
}

public String getStudent_number() {
    return student_number;
}

public String getFaculty() {
    return faculty;
}

public void setName(String name) {
    this.name = name;
}

public boolean isAdmin() {
    return isAdmin;
}

public void setAdmin(boolean admin) {
    isAdmin = admin;
}

public void setStudent_number(String student_number) {
    this.student_number = student_number;
}

public void setFaculty(String faculty) {
    this.faculty = faculty;
}
}

最佳答案

不需要 onDataChange() 方法中的循环:

public void onDataChange(DataSnapshot dataSnapshot) {
    if (ds.exists()) {
        UserInformation userInformation = ds.child(id).getValue(UserInformation.class);
        String name = userInformation.getName();
        mWelcomeUserMessage.setText(name);
        //logging name
        Log.d("My activity", "name of user with userID - " + id + " is : " + userInformation.getName());
    }
}

只有当您使用带有值监听器的查询时才需要循环,因为一个查询可以有多个结果。但在您的情况下,您直接访问正确的子节点,因此不需要循环。

关于android - 如何检索 firebase 数据库条目的细节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44366818/

相关文章:

android - 为什么这一行 xmlns :android ="http://schemas.android.com/apk/res/android" must be the first in the layout xml file?

Android 滑动 - 我应该使用手势监听器还是 MotionEvent.ACTION_MOVE?

javascript - 使用 firebase 在 ionic 2 中实现类似/不同的功能

java - 在没有FirebaseRecyclerAdapter的情况下如何增加Firebase中每个位置的viewCount?

javascript - 类型错误 : Cannot read property 'match' of undefined when calling Firebase Cloud Function

javascript - firebase -> 日期顺序反转

java - 重新启动 Activity 后的 NPE

android - OnSuccess 没有被命中,获取不到图片下载地址,fire base

swift - 在 Firebase 中检索 Childs Child - 解包 unique-post-ids

firebase - 错误 : Wrap function is only available for `onCall` HTTP functions, 不是 `onRequest`