gpt4 book ai didi

java - 从 firebase 检索数据时为空值

转载 作者:行者123 更新时间:2023-11-29 02:32:57 24 4
gpt4 key购买 nike

enter image description here我有一个包含多个用户的考勤应用程序,我正在尝试检索一些用户详细信息并将其显示到列表名称/姓氏/empId。执行我的代码时,它会检索并显示名字,但不会显示其他 2 个值。调试时,值显示为 NULL,我看不到我错过了什么。希望一些新鲜的眼睛可以帮助

非常感谢任何帮助 - 下面的代码带有图片

[//Activity code to get data

protected void onStart() {
super.onStart();
databaseOut.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userList.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
User user = postSnapshot.getValue(User.class);
userList.add(user);

}
zz_List_Users userListAdapter = new zz_List_Users(updateEmployeeDetailsActivity.this, userList);
listViewUsers.setAdapter(userListAdapter);
}

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

//我要获取并添加到列表中的数据的用户列表代码

public class zz_List_Users extends ArrayAdapter<User> {
private Activity context;
List<User> userList;

public zz_List_Users(Activity context, List<User> userList) {
super(context, R.layout.zz_list_user, userList);
this.context = context;
this.userList = userList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();

View listViewItem = inflater.inflate(R.layout.zz_list_user, null, true);

TextView textViewNameU = (TextView) listViewItem.findViewById(R.id.textViewNameU);
TextView textViewSurname = (TextView) listViewItem.findViewById(R.id.textViewSurnameU);
TextView textViewEmpId = (TextView) listViewItem.findViewById(R.id.textViewEmpIdU);

User user = userList.get(position);

textViewSurname.setText(user.getSurname());
textViewEmpId.setText(user.getEmpId());
textViewNameU.setText(user.getName());


return listViewItem;
}
}

//用户类

public class User {
private String userId;
private String name;
private String surname;
private String empId;
private String password;
private String address;
private String postCode;
private String userEmail;
public boolean adminUser;


public User(){

}

public User(String userId, String name, String surname, String empId, String password, String address, String postCode, String userEmail, boolean adminUser) {
this.userId = userId;
this.name = name;
this.surname = surname;
this.empId = empId;
this.password = password;
this.address = address;
this.postCode = postCode;
this.userEmail = userEmail;
this.adminUser = adminUser;


}

public String getUserId() {
return userId;
}

public String getName() {
return name;
}

public String getSurname() {
return surname;
}

public String getEmpId() { return empId; }

public String getPassword() {
return password;
}

public String getAddress() { return address; }

public String getPostCode() {return postCode; }

public String getUserEmail() {return userEmail; }

public boolean getAdminUser() {return adminUser; }
}

非常感谢任何帮助

//Firebase JSON

{
"users" : {
"1mXbSGCzGmbCp7lLXjxjX2kihHI3" : {
"address" : "31 moyola drive shantallow",
"adminUser" : true,
"employeeId" : "902488",
"firstName" : "Sarah",
"password" : "solskjaer",
"postCode" : "bt48 0GE",
"surname" : "Breslin",
"userEmail" : "sarahbreslin81@hotmail.com",
"userId" : "1mXbSGCzGmbCp7lLXjxjX2kihHI3"
},
"O0MYu0YmENgodYSHU0BkMSFQCFj2" : {
"address" : "123 Elaghmore Park",
"adminUser" : true,
"employeeId" : "902499",
"firstName" : "Seamus",
"password" : "solskjaer",
"postCode" : "BT48 8DY",
"surname" : "Ferry",
"userEmail" : "seamyferry@hotmail.co.uk",
"userId" : "O0MYu0YmENgodYSHU0BkMSFQCFj2"
}
}
}

最佳答案

您的字段名称必须与 Firebase 响应中的名称相同。

因此在 User 类中将 name 更改为 firstName,如您的响应所示。也 empIdemployeeId

其他领域同理。

所以使用这个tool生成您的 POJO 以避免此类问题。

有一个有用的plugin在 Android Studio 中也是如此。

关于java - 从 firebase 检索数据时为空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48633311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com