简体   繁体   English

FirebaseDatabase 显示来自单个用户的单个值

[英]FirebaseDatabase display a single value from a single user

Working on a small project at the moment on Android Studio and can't work out how I can print a single value from the current user.目前在 Android Studio 上处理一个小项目,无法弄清楚如何从当前用户打印单个值。 I am trying to get the current user's username to print, without printing all of the users other information such as email address...我正在尝试打印当前用户的用户名,而不打印所有用户的其他信息,例如电子邮件地址...

Here is my database这是我的数据库

在此处输入图片说明

I know I've got duplicates of everything, I've fixed that since :P我知道我有所有东西的副本,我已经修复了:P

So far I've managed to print all of the user's details fine (unfortunately every user's details also printed)... using this code:到目前为止,我已经成功地打印了所有用户的详细信息(不幸的是,每个用户的详细信息也都打印了)……使用以下代码:

fDatabase = FirebaseDatabase.getInstance().getReference().child("Users");

    fDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String test = (String) dataSnapshot.getValue().toString();
            Log.d ("Testing", test);

在此处输入图片说明 The output I achieved can be found above.我实现的输出可以在上面找到。

and this is my User class:这是我的用户类:

class User {
String Displayname;
String Email;
Long createdAt;

public User () {}

public User(String displayname, String email, Long createdAt) {
    this.Displayname = displayname;
    this.Email = email;
    this.createdAt = createdAt;
}

public String getDisplaynames() {
    return Displayname;
}

public String getEmails() {
    return Email;
}

public Long getCreatedAts() {
    return createdAt;
}

My question is how can I get my output to print just: Testing: Chrisharvz我的问题是如何让我的输出只打印:测试:Chrisharvz

The reason I want to do this is so I can display the user's username as they log into my application, replacing Log.d with Textview.settext etc...我想这样做的原因是我可以在用户登录我的应用程序时显示用户的用户名,用 Textview.settext 等替换 Log.d...

Anyone have an idea?有人有想法吗? If you need anymore information, lemme know!如果您需要更多信息,让我知道! :D Thanks! :D 谢谢!

What I suggest is convert the data back into user object after you retrieve the data from firebase because right now you are using the whole object.我的建议是在您从 firebase 检索数据后将数据转换回用户对象,因为现在您正在使用整个对象。 You should try this:你应该试试这个:

    fDatabase =FirebaseDatabase.getInstance().getReference().child("Users");

    fDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // convert the data back to the model
            User user = (User) dataSnapshot.child(currentUser.getUid()).getValue(User.class);
            // just get the string value for the username
            String displayName = user.getDisplayName() // getter method from your model
            Log.d ("Testing", displayName);
            }

Hopefully this helps your problem.希望这可以帮助您解决问题。

Assuming that Users node is a direct child of your Firebase database root and 0x2s4Ft... is the uid of the logged in user, to get the value only from your Displayname field, please use the following code:假设Users节点是您的 Firebase 数据库根目录的直接子节点,并且0x2s4Ft...是登录用户的uid ,要仅从您的Displayname字段中获取值,请使用以下代码:

FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String uid = firebaseUser.getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Users").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String Displayname = dataSnapshot.child("Displayname").getValue(String.class);
        Log.d("Testing: ", Displayname);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(eventListener);

Your output will be:您的输出将是:

Testing: Chrisharvz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM