我正在尝试从我的Firebase数据库中检索多个键/值,当一个键具有一个值时,它工作正常,但是当一个键具有多个键和值对时(检查我的Firebase数据库-连接),我无法检索在我的应用中
注意:userEmail,userName等下的1,2 ....是自动生成的,因此我很难理解如何创建我的课程。
下面是我一直在尝试找出的代码。
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
User h = dataSnapshot1.getValue(User.class);
list.add(h);
}
adapter = new User_Adapter(User_Activity.this, list);
recyclerView.setAdapter(adapter);
}
下面是我的用户类别
public class User {
private String userEmail;
private String userName;
private String userPassword;
public User(){}
public User(String userEmail, String userName, String userPassword) {
this.userEmail = userEmail;
this.userName = userName;
this.userPassword = userPassword;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
} }
以下是我的适配器类
public class User_Adapter extends RecyclerView.Adapter<User_Adapter.ViewHolder> {
Context context;
ArrayList<User> users;
public User_Adapter(Context c, ArrayList<User> h){
context = c;
users = h;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.user_content,parent,false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.userEmail.setText(users.get(position).getUserEmail());//.setText(users.get(position).getUserEmail());
holder.userName.setText(users.get(position).getUserName());
holder.userPassword.setText(users.get(position).getUserPassword());
}
@Override
public int getItemCount() {
return users.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView userEmail, userName, userPassword;
public ViewHolder(View itemView) {
super(itemView);
userEmail = (TextView) itemView.findViewById(R.id.account_recycler);
userName = (TextView) itemView.findViewById(R.id.amount_recycler);
userPassword = (TextView) itemView.findViewById(R.id.password_recycler);
}
} }
我需要帮助来了解如何更改我的类结构,以便可以读取firebase数据库。
答案 0 :(得分:1)
我不明白您的数据结构为什么会这样。您是否希望像一个用户这样的功能可以创建许多userEmail,userName? 如果您只想将每个用户数据保存在更好的数据库结构中,例如:
Users
-user1
userEmail:Imad@test.com
userName:Imad
userPassword:123456
-user2
userEmail:suhail@test.com
userName:suhail
userPassword:123456
这也是您创建的用户类。
或者您希望一个用户可以创建多封电子邮件。
Users
-userid1
-pushId1
userEmail:Imad@test.com
userName:Imad
userPassword:123456
-pushId2
userEmail:suhail@test.com
userName:suhail
userPassword:123456