我想根据消息ID号接收发送给用户的消息。但是,我无法解决No setter / field错误。
如何解决此问题?什么方法?
public class Messages {
private String sendMessage;
private long timestamp;
public String getSendMessage() {
return sendMessage;
}
public void setSendMessage(String sendMessage) {
this.sendMessage = sendMessage;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}}
public void getMessage(){
databaseReference = FirebaseDatabase.getInstance().getReference("Users").getRef();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
messagesList = new ArrayList<>(); //Initialize the list
for (DataSnapshot postSnapShot : dataSnapshot.getChildren()) {
Messages messages = postSnapShot.child("messages").child(auth.getUid()).child("message").getValue(Messages.class);
messagesList.add(messages);
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {}
});
}
我只想获取消息和时间戳。但是我遇到了错误;
/com.****.chat W/ClassMapper: No setter/field for 05102018025211 found on class com.****.chat.Model.Messages
No setter/field for 05102018024849 found on class com.onsoftware.chat.Model.Messages
答案 0 :(得分:0)
要解决此问题,请使用以下代码行:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference messageRef = rootRef.child("Users").child(uid).child("message").child(toUid).child("message");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Messages> messagesList = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Messages messages = ds.getValue(Messages.class);
messagesList.add(messages);
Log.d("TAG", messages.getSendMessage());
}
//Do what you need to do with your messagesList
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
messageRef.addListenerForSingleValueEvent(valueEventListener);
其中toUid
是接收消息的用户的uid。
如果您想在某个时候尝试Cloud Firestore,可以在这里找到有关如何创建 Chat App using Kotlin 的教程。