简体   繁体   English

如何从Firebase数据库读取用户信息并在Android App中显示为列表

[英]How to a read user Information from Firebase database and display as a List in Android App

I am new here and I'd be glad if anyone can help me.What I want to do is get UID and Username of all the users and display it in a list. 我是新来的,很高兴有人能帮助我。我要做的就是获取所有用户的UID和用户名并将其显示在列表中。

Here is my related code: 这是我的相关代码:

mDatabase = FirebaseDatabase.getInstance().getReference();
        listView = (ListView) v.findViewById(R.id.listview);

        mDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {


                for(DataSnapshot user : dataSnapshot.getChildren()){

                    **// Not sure what to do here**

                    array.add(username);
                   }

              ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, array);
              listView.setAdapter(adapter);
               }

My Users Class which I use to Insert User Information 我用来插入用户信息的用户Class

public class User {

    public String username;
    public String phone;

    public User() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

    public User(String username, String phone) {
        this.username = username;
        this.phone = phone;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPhone() {
        return phone;
    }


}

And this is how my Database Looks like Firebase Database 这就是我的数据库看起来像Firebase数据库的样子

Please use this code: 请使用以下代码:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String phone = ds.child("phone").getVelue(String.class);
            String username = ds.child("username").getValue(String.class);
            Log.d("TAG", phone + " / " + username);
        }
    }

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

And your uotput will be: 您的投入量将为:

uo / pol
809151699 / shivam
//and so on

Hope it helps. 希望能帮助到你。

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

相关问题 如何从Firebase数据库读取并将信息存储在列表视图中? Android的 - How can I read from a firebase database and store the information within a list view? Android 如何在Android上从Firebase列出用户信息? - How to list users information from firebase on android? 如何从实时数据库[Firebase]中读取和显示? - How to read and display from realtime database [Firebase]? 如何从注册用户那里获取特定信息并将其显示在 Kotlin 和 Firebase 的应用程序中? - How do I get specific information from a registered user and display it in my app in Kotlin with Firebase? 如何在firebase数据库android中存储用户配置文件信息 - How to store user profile information in firebase database android 如何从Firebase数据库读取数据到列表 - How to read data from Firebase database to a List 如何使用Android从Firebase数据库显示密钥? - How to display the keys from a Firebase Database with Android? 如何在Firebase中同时登录并从数据库中获取用户信息 - How to sign in and get user information from database at same time in firebase 我的Android应用无法从Firebase数据库读取数据 - My Android App won't read data from Firebase Database 如何从 Android 应用程序中删除 Firebase 用户? - How to delete a Firebase user from Android App?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM