如何列出Android(Firebase)中的注册用户名? [英] How to list the registered users names in Android (Firebase)?

查看:118
本文介绍了如何列出Android(Firebase)中的注册用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚接触android,刚刚学会创建一个像Facebook或Instagram的用户配置文件,目前我已经注册了四个用户的电子邮件/密码,并在数据库中存储电子邮件,姓名,电话。在另一个活动中,我试图只提取注册用户的名字,如Facebook上的你可能认识的人,但是它没有显示任何内容。

 <$ (DataSnapshot ds:dataSnapshot.getChildren()){

用户信息userInformation = new UserInformation();

userInformation.setName(ds.child(users)。child(name)。getValue(UserInformation.class).getName());

ArrayList< String> array = new ArrayList<>();

array.add(userInformation.getName());

ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);

mListView.setAdapter(adapter);






是正确的获取方式,因为我得到了NullPointerException, UserInformation是包含getter和setter的模型类。

$ $ $ $ $ $ $ $
$ KWE45gijkfJDK6782IBkjas(UID)
电子邮件:@ example.com
名称:示例
phone_num:10000000

一旦用户注册,这就是我如何将数据存储到数据库,
现在像这样我有四个用户,我想提取每个单独的名称在单独的listView。

解决方案



  DatabaseReference rootRef = FirebaseDatabase.getInstance()。getReference(); 
DatabaseReference usersdRef = rootRef.child(users);
ValueEventListener eventListener = new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
for(DataSnapshot ds:dataSnapshot.getChildren()){
String name = ds.child(name)。getValue(String.class);
Log.d(TAG,name);
}
}

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

输出结果就是这个用户名。



希望有帮助。


I am new to android and just learned to create a users profile like facebook or instagram, currently I have registered four users with email/password and in database am storing there email, name, phone. In another activity am trying to fetch only the names of the registered users like "People you may know" in facebook, but it is displaying nothing.

private void showData(DataSnapshot dataSnapshot) {

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

        UserInformation userInformation = new UserInformation();

        userInformation.setName(ds.child("users").child("name").getValue(UserInformation.class).getName());

        ArrayList<String> array = new ArrayList<>();

        array.add(userInformation.getName());

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, array);

        mListView.setAdapter(adapter);

    }

is this correct way of fetching it because am getting NullPointerException, UserInformation is model class containing getters and setters.

fire-18b42
           users
                KWE45gijkfJDK6782IBkjas(UID)
                    email: "@example.com"
                    name: "Example"
                    phone_num: 10000000

This is how I have stored the data to database once the user is registered, now like this I have four users and I want to extract every ones name in seperate listView.

解决方案

To display those names, please use this code:

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

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

And the output will be in this case all your user names.

Hope it helps.

这篇关于如何列出Android(Firebase)中的注册用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆