我需要获取模型类列表中的数据快照,但现在我得到空值。当我调试时,我发现我的模型类没有实际从我的satasnapshot和添加消息中获取数据
我正在使用的代码。
model class
package com.teamta.artiando.models;
public class MatchModel {
private String last_message;
public String last_message_sender_id;
public Object last_message_timestamp;
public Object timestamp;
public User user1;
public User user2;
public MatchModel(String last_message, String last_message_sender_id, Object last_message_timestamp, Object timestamp, User user1, User user2) {
this.last_message = last_message;
this.last_message_sender_id = last_message_sender_id;
this.last_message_timestamp = last_message_timestamp;
this.timestamp = timestamp;
this.user1 = user1;
this.user2 = user2;
}
public MatchModel(){
}
public String getLast_message() {
return last_message;
}
public void setLast_message(String last_message) {
this.last_message = last_message;
}
public String getLast_message_sender_id() {
return last_message_sender_id;
}
public void setLast_message_sender_id(String last_message_sender_id) {
this.last_message_sender_id = last_message_sender_id;
}
public Object getLast_message_timestamp() {
return last_message_timestamp;
}
public void setLast_message_timestamp(Object last_message_timestamp) {
this.last_message_timestamp = last_message_timestamp;
}
public Object getTimestamp() {
return timestamp;
}
public void setTimestamp(Object timestamp) {
this.timestamp = timestamp;
}
public User getUser1() {
return user1;
}
public void setUser1(User user1) {
this.user1 = user1;
}
public User getUser2() {
return user2;
}
public void setUser2(User user2) {
this.user2 = user2;
}
public static class User {
private String name;
public String profilepic_url;
public String userId;
public User(String name, String profilepic_url, String userId) {
this.name = name;
this.profilepic_url= profilepic_url;
this.userId =userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProfilepic_url() {
return profilepic_url;
}
public void setProfilepic_url(String profilepic_url) {
this.profilepic_url = profilepic_url;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
}
在我的经理班上我使用
private ValueEventListener userCalueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
MatchModel messages = dataSnapshot.getValue(MatchModel.class);
name.add(messages);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("year", "DatabaseError: "+databaseError);
}
};
这是我的查询
public void observeUserdata() { FirebaseDatabase.getInstance().getReference().child("matches").orderByChild(userId).equalTo(true).addValueEventListener(userCalueEventListener);
}
我的firbase看起来像
matches
|
|-kwerrwuw8iaajasd // i have 30 key like this key in the matches in this node
|- Kweyatsgdywywyww
|- last_message
|- last_senderuserid
|- timestamp
|-user1
| |- name
| |- profile_pic
| |- user_id
|-user2
| |- name
| |- profile_pic
| |- user_id
|- Kweyatsgdywywyww
答案 0 :(得分:1)
你需要在onDataChange()方法中启动一个循环并逐个访问你的数据并放入你的列表,然后你就可以得到消息列表。
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot data: dataSnapshot.getChildren()){
yourClass obj=data.getValue(yourClass.class);
yourList.add(obj);
}
}
答案 1 :(得分:0)
您收到null
,因为您使用了错误的DatabaseReference
。
FirebaseDatabase.getInstance().getReference()
.child("matches")
.orderByChild(userId)
.equalTo(true).addValueEventListener(userCalueEventListener);
正如我在您的数据库结构中看到的那样,您正在尝试按用户对象对子项进行排序,而您无法以这种方式进行查询。您可以做的是在timestamp
后查询,matches
是FirebaseDatabase.getInstance().getReference()
.child("matches")
.orderByChild(userId)
.equalTo(true).addValueEventListener(userCalueEventListener);
节点的直接子节点。您的查询应如下所示:
return holder
答案 2 :(得分:0)
您需要在所有模型类中添加一个空构造函数, 如果它们是模型类中的内部类,则还需要向该类添加一个空构造函数