简体   繁体   English

Android Studio:如何从 Firebase 检索特定数据?

[英]Android studio : How do I retrieve specific data from Firebase?

private TextView welcomeText;
DatabaseReference databaseReference = database.getReference("Users").child("name");

https://i.stack.imgur.com/Qayc6.png https://i.stack.imgur.com/Qayc6.png

What I want to do is retrieve the data "name" from the Firebase database and setText for the welcomeText to the data "name".我想要做的是从 Firebase 数据库中检索数据“名称”并将 WelcomeText 的 setText 检索到数据“名称”。

Try this尝试这个

 DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

    reference.child("Users").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                //model class assign
                ModelClass Class = dataSnapshot.getValue(ModelClass.class);
                String name = cartClass.getName();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });

Hey I hope you going fantastic.嘿,我希望你会很棒。 Make sure you have inserted your DB URL from Firebase in your FirebaseDatabase object in your code before coding the query.在编码查询之前,请确保您已在代码中的 FirebaseDatabase 对象中插入来自 Firebase 的数据库 URL。 So in order your project is ready you need to read the following path: DB > users > UserID > name to reach the value you are looking for.因此,为了准备好您的项目,您需要阅读以下路径: DB > users > UserID > name以达到您正在寻找的值。 If you want to change 'name' into all of your firebase database registers you should try this.如果您想将“名称”更改为您的所有 firebase 数据库寄存器,您应该试试这个。

 DatabaseReference reference = FirebaseDatabase.getInstance('FirebaseDBURL').getReference();

reference.child("Users").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
            //Here you are reading all of your Users objects
            if(dataSnapshot.getKey.equals('name')){ //Here we are identifying the attr you want to change by its key name
                reference.child("Users").child(datasnapshot.getValue()).setValue(welcomeText);
            }
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {

    }
});

I hope you solve your issue.我希望你能解决你的问题。 Nevertheless if you only want to change one of your user's info you can use his/her ID directly without using for in your code and check every user in your system.不过,如果您只想更改用户的一个信息,您可以直接使用他/她的 ID,而无需在代码中使用 for,并检查系统中的每个用户。 reference.child("Users").child(USER_ID).setValue(welcomeText);

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

相关问题 如何在firebase数据库中检索特定孩子的数据 - android studio/java? - How to retrieve data for a specific child in firebase database - android studio/ java? 如何在 Android Studio 中从实时 Firebase 中检索特定用户 - How to retrieve specific user from realtime Firebase in Android Studio 如何使用 Android Studio 从 Firebase 实时数据库中的节点检索特定数据? - How to retrieve a specific data from a node in Firebase Realtime Database using Android Studio? 将数据从 firebase 检索到 android studio 作为列表视图 - Retrieve data from firebase to android studio as listview 在 android 工作室中检索 Firebase 回收视图中特定值的数据 - retrieve data of specific value in Firebase recycle view in android studio 如何从android的firebase数据库中的uid获取/检索多个数据? - How do I get/retrieve multiple data from uid in the firebase database in android? 从android studio中的firebase获取特定数据 - Fetching specific data from firebase in android studio 如何从firebase检索特定的数据列表? - How to retrieve specific list of data from firebase? 如何从firebase检索特定的数据列表 - How to retrieve specific list of data from firebase 如何在代码中的确切位置将数据从 firebase 提取到 android studio? - How do I pull data from firebase into android studio at the exact point in the code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM