在java类中,我使用了一些随机的用户名和全名进行测试,但现在我想获取Firebase的Mates节点中当前用户的子用户名和用户名列表。
public class User
{
public String fullname;
public String username;
public User()
{
FirebaseAuth mAuth=FirebaseAuth.getInstance();
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Mates").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<User> USERS = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
User user = ds.getValue(User.class);
USERS.add(user);
Log.d(TAG, user.getUsername() + " / " + user.getFullname());
}
//Do what you need to do with your userList
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
}
public User(String fullname, String username) {
this.fullname = fullname;
this.username = username;
}
public String getFullname() {
return fullname;
}
public String getUsername() {
return username;
}
public static List<User> USERS ;
}
我想将此用户替换为当前用户的子级的Firebase用户,在这里放置我的数据库映像。
答案 0 :(得分:0)
明确表示您要将Java类的全名和用户名替换为firebase。 对 ?
答案 1 :(得分:0)
您的班级用户属性可能是private
,否则您会从Firebase获取数据时出错。
访问此链接以使用列表https://firebase.google.com/docs/database/android/lists-of-data?authuser=0
答案 2 :(得分:0)
According to your comment, to get all user objects that exist within Mates/uid
, please use the following lines of code:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("Mates").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<User> userList = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
User user = ds.getValue(User.class);
userList.add(user);
Log.d(TAG, user.getUsername() + " / " + user.getFullname());
}
//Do what you need to do with your userList
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
uidRef.addListenerForSingleValueEvent(valueEventListener);
The output in the logcat will be all usernames and fullnames of all users that exist within the uid.