在我的应用中,我创建了登录和注册页面,并将用户名和电子邮件保存到我的Firebase数据库。我还用片段做了底部导航。我想从Firebase获取这些值(名称,邮件)并将其放入配置文件片段中的TextView。当我尝试这个时,我的应用程序崩溃了。我该如何解决这个问题?
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;
}
}
答案 0 :(得分:0)
在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;
}
答案 1 :(得分:0)
假设您的数据库结构与此类似:
Firebase-root
|
--- users
|
--- uid
|
--- name: "John"
|
--- email: "john@email.com"
要打印姓名和电子邮件,请使用以下代码使用模型类:
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);
如果您想使用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);
在这两种情况下,输出都是:John / john@email.com