所以我在从firebase数据库中检索值时遇到了问题。基本上,我想获得与"用户名"相关联的值。键。但是,我当前的代码只返回任何内容。当我通过省略.child("用户名")更改我的代码时,dataSnapshotTwo包含电子邮件和用户名。我在这里做错了什么?
这是我的代码:uniqueuserid是随机字符的字符串,例如" cUkHiXwIaXOqigeoRPuh3Yc9oau2"
public void sendFriendRequest(final String userID){
mDatabase.child("Users").child(uniqueUserID).child("username")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshotTwo) {
for (DataSnapshot miniSnapshot : dataSnapshotTwo.getChildren()) {
String currentUserName = (String) miniSnapshot.getValue();
mDatabase.child("Users").child(userID).child("Requests").child(uniqueUserID).setValue(currentUserName);
}
}
@Override
public void onCancelled(DatabaseError firebaseError) {
Log.e("MainActivity", "onCancelled", firebaseError.toException());
}
});
}
这是我的数据库结构:
{"Users" :
{
"User_1" : {
"Called" : {
"Fritz" : 1
},
"City" : "Groningen",
"Friends" : {
"Fritz" : 50,
"Jos" : 0
"Requests" : {
"zuipmaat_1" : 0
"email" : "b@hotmail.com",
"username" : "Bert"
"cUkHiXwIaXOqigeoRPuh3Yc9oau2" : {
"Requests" : {
"cUkHiXwIaXOqigeoRPuh3Yc9oau2" : "Tommie"
"email" : "t2@live.nl",
"username" : "Tommie"
"jAYc9IzcBRRuXZCxYEncEIRrPw72" : {
"Requests" : {
"cUkHiXwIaXOqigeoRPuh3Yc9oau2" : "Tommie "
"email" : "t@hotmail.com",
"username" : "Tom"
答案 0 :(得分:0)
这看起来更漂亮
{
"Users": {
"User_1": {
"Called": {
"Fritz": 1
},
"City": "Groningen",
"Friends": {
"Fritz": 50,
"Jos": 0
},
"Requests": {
"zuipmaat_1": 0
},
"email": "b@hotmail.com",
"username": "Bert"
},
"cUkHiXwIaXOqigeoRPuh3Yc9oau2": {
"Requests": {
"cUkHiXwIaXOqigeoRPuh3Yc9oau2": "Tommie"
},
"email": "t@live.nl",
"username": "Tommie"
},
"jAYc9IzcBRRuXZCxYEncEIRrPw72": {
"Requests": {
"cUkHiXwIaXOqigeoRPuh3Yc9oau2": "Tommie"
},
"email": "tom@hotmail.com",
"username": "Tom"
}
}
}
答案 1 :(得分:0)
如果我理解你的意图......
首先,更好地处理对象。
public class User {
public Called Called;
public String City;
public ArrayList<Friend> Friends;
public ArrayList<Requests> Requests;
public String email;
public String username;
public class Called{
public String name;
public int sum;
}
public class Friend{
public String name;
public int sum;
}
public class Requests{
public String name;
public int sum;
}
}
第二,尝试更新/插入所有对象
User user = new User();
user.City = ..... /*populate the object*/
database.getReference("Users").push().setValue(user); // generate the user Id automatically
或
database.getReference("Users").child(userID).setValue(user); // generate your own user Id
第三,检索数据
public void sendFriendRequest(final String userID) {
mDatabase.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshotTwo) {
for (DataSnapshot miniSnapshot : dataSnapshotTwo.getChildren()) {
User user = (User) miniSnapshot.getValue();
System.out.println(user.username); // print the username
}
}
@Override
public void onCancelled(DatabaseError firebaseError) {
Log.e("MainActivity", "onCancelled", firebaseError.toException());
}
});
}
从不测试它。希望这很有帮助。
答案 2 :(得分:0)
要获取用户名,请使用以下代码:
public void sendFriendRequest(final String userID){
mDatabase.child("Users").child(uniqueUserID).child("username").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshotTwo) {
String currentUserName = dataSnapshotTwo.getValue(String.class);
Log.d("TAG", currentUserName);
}
@Override
public void onCancelled(DatabaseError firebaseError) {
Log.e("MainActivity", "onCancelled", firebaseError.toException());
}
});
}
正如您可能看到的那样,我已经删除了for循环,因为不需要。