Fragment的Oncreate
public class HomeScreen_Profile extends Fragment {
EditText Name, Email, Status, Quote;
private FirebaseAuth mAuth;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
Name = (EditText) rootView.findViewById(R.id.Name);
Email = (EditText) rootView.findViewById(R.id.Email);
Status = (EditText) rootView.findViewById(R.id.Status);
Quote = (EditText) rootView.findViewById(R.id.Quote);
mAuth = FirebaseAuth.getInstance();
loadUserInformation();
return rootView;
}
}
LoadUserInfo方法
public void loadUserInformation() {
final String uid = mAuth.getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child(uid);
DatabaseReference uidRef = rootRef.child("ref").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
SetUpProfileHelper obj = postSnapshot.getValue(SetUpProfileHelper.class);
String UserName = obj.getName();
String UserEmail = obj.getEmail();
Name.setText(UserName);
Email.setText(UserEmail);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(), "Error Loading UserDetails", Toast.LENGTH_LONG).show();
}
};
uidRef.addListenerForSingleValueEvent(eventListener);
}
为了检查工作原理,我在方法中只使用了getName和getEmail。我没有问题保存userinfo,但是获取它给我带来了困难,因为互联网上的所有教程都在获取它并在我不想要的ListView中显示它。有人能指出这段代码中的错误吗?
答案 0 :(得分:0)
位置错误,您在数据库中有这个:
rootnode
Users
uid
name:Userx
email:userx@gmail.com
phonenumber: number_here
您希望得到name
和email
然后执行此操作:
public void loadUserInformation() {
final String uid = mAuth.getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference().child("Users");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot: dataSnapshot.getChildren()){
String name=postSnapshot.child("Name").getValue().toString();
String email=postSnapsoht.child("Email").getValue().toString()
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(), "Error Loading UserDetails", Toast.LENGTH_LONG).show();
}
};
rootRef.addListenerForSingleValueEvent(eventListener);
}
由于uid
位于Users
节点下(因此直接child
)并且因为您想要检索uid
,所以只需执行for循环就可以获取您需要的数据。