简体   繁体   English

我无法从Firebase获取值并将它们放在片段中的TextView中

[英]I cant get the values from Firebase and put them in to TextView in fragment

In my app, I created login and signup page and saved the user name and email to my Firebase database. 在我的应用程序中,我创建了登录和注册页面,并将用户名和电子邮件保存到我的Firebase数据库中。 I also made bottom navigation with fragment. 我还用片段进行了底部导航。 I want to take these values(name, mail) from Firebase and put it into TextView in profile fragment. 我想从Firebase中获取这些值(名称,邮件),并将其放入配置文件片段的TextView中。 When I tried this, my app crashed. 当我尝试此操作时,我的应用程序崩溃了。 How can I solve this problem? 我怎么解决这个问题?

public class ProfileFragment extends Fragment {
    private FirebaseDatabase mFirebaseDatabase;
    private FirebaseAuth mAuth;
    private String userID;

    private TextView tName;
    private TextView tEmail;


    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View profileView = inflater.inflate(R.layout.fragment_profile, null);

        tName=profileView.findViewById(R.id.tv_name);
        tEmail=profileView.findViewById(R.id.tv_email);


        mAuth=FirebaseAuth.getInstance();
        mFirebaseDatabase=FirebaseDatabase.getInstance();
        final DatabaseReference myRef=mFirebaseDatabase.getReference("users");
        FirebaseUser user=mAuth.getCurrentUser();
        userID=user.getUid();

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds: dataSnapshot.getChildren()){
                    User user=new User();
                    user.setUserName(ds.child(userID).getValue(User.class).getUserName());
                    user.setUserEmail(ds.child(userID).getValue(User.class).getUserEmail());

                    tName.setText(user.getUserName());
                    tEmail.setText(user.getUserEmail());
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });

        return profileView;
    }
}

create file in xml folder 在xml文件夹中创建文件

<?xml version="1.0" encoding="utf-8"?>
<defaultsMap>
    <entry>
       <key>email</key>
       <value>name</value>
</entry>

public FirebaseRemoteConfig getmFirebaseRemoteConfig() {

    mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance();

    FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder()
             //.setDeveloperModeEnabled(BuildConfig.DEBUG)
            .setDeveloperModeEnabled(false)
            .build();
    mFirebaseRemoteConfig.setConfigSettings(configSettings);

    mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);


    return mFirebaseRemoteConfig;
}

Assuming that your database structure looks similar to this: 假设您的数据库结构与此类似:

Firebase-root
   |
   --- users
         |
         --- uid
              |
              --- name: "John"
              |
              --- email: "john@email.com"

To print the name and the email, please use the following code using a model class: 要打印姓名和电子邮件,请使用以下代码和模型类:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        User user = dataSnapshot.getValue(User.class);
        String name = user.getUserName();
        tName.setText(name);
        String email = user.getUserEmail();
        tEmail.setText(email);
        Log.d("TAG", name + " / " + email);
    }

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

If you want to print them even simpler using the String class, please use the following code: 如果要使用String类将它们打印得更简单,请使用以下代码:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("users").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String name = dataSnapshot.child("name").getValue(String.class);
        tName.setText(name);
        String email = dataSnapshot.child("email").getValue(String.class);
        tEmail.setText(email);
        Log.d("TAG", name + " / " + email);
    }

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

In both cases, the output will be: John / john@email.com 在这两种情况下,输出均为: John / john@email.com

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

相关问题 我无法从子类中获取构造函数以读取放入其中的字符串 - I cant get my constructors from my subclasses to read the Strings I have put in them 从Firebase Realtime数据库中获取值,并将其放入ArrayList中 - Grab values from the Firebase Realtime database and put them in an ArrayList 我无法使用从 Activity 传递到片段的捆绑字符串数据在 textView 中设置文本 - I cant set text in textView by using bundle String data that pass from Activity to fragment 如何从哈希映射中获取值并将其放入数组? - how to get the values from hash map and put them in array? 为什么我无法将这些计算结果显示在textview上? - Why cant i get these calculations to display on a textview? 我无法将所有数据从Firebase获取到Listview - I cant get all the data from firebase to my Listview 我想使用回收者视图将数据从Firebase获取到内部片段 - I want to get data from firebase to inside fragment with recycler view 无法从活动到片段获取捆绑价值 - Cant get bundle value from activity to fragment 从JSON获取所有用户并将其放入TextView - Get all users from JSON and put it into TextView 如何从片段本身中更改片段的文本视图? - How do I change the textview of a Fragment from WITHIN the fragment itself?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM