gpt4 book ai didi

java - for 循环阻止我从数据库检索数据?

转载 作者:行者123 更新时间:2023-12-02 01:00:16 25 4
gpt4 key购买 nike

尝试从 Firebase 检索数据以使用用户的 myID 填充屏幕, userID ,和imageURL这对于每个用户来说都是唯一的。我有以下方法readMessage();这是从数据库中检索该信息的方法,但它无法正常工作。

这就是 Logcat 告诉我的 Can't convert object of type java.lang.String to type com.e.chatapp.Model.Chat .

起初我试图摆脱 for Loop因为在过去这让它工作得很好,但这次却没有帮助。有人可以指出我的代码中的错误是什么吗?

问题是 if声明。

下面是我的MessageActivity.java和模型类Chat.java

enter image description here

MessageActivity.java

private void sendMessage(String sender, String receiver, String message) {

DatabaseReference reference = FirebaseDatabase.getInstance().getReference();

HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("sender", sender);
hashMap.put("receiver", receiver);
hashMap.put("message", message);

reference.child("Chats").child(mFirebaseUser.getUid()).setValue(hashMap);
}

private void readMessage(final String myID, final String userID, final String imageURL) {
mChats = new ArrayList<>();

mReference = FirebaseDatabase.getInstance().getReference("Chats");
mReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mChats.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Chat chat = snapshot.getValue(Chat.class);
if (chat.getReceiver().equals(myID) && chat.getSender().equals(userID) ||
chat.getReceiver().equals(userID) && chat.getSender().equals(myID)) {
mChats.add(chat);
}

mMessageAdapter = new MessageAdapter(MessageActivity.this, mChats, imageURL);
mRecyclerView.setAdapter(mMessageAdapter);
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

也尝试过这样

 private void readMessage(final String myID, final String userID, final String imageURL) {
mChats = new ArrayList<>();

mReference = FirebaseDatabase.getInstance().getReference("Chats");
mReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mChats.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Chat chat = snapshot.getValue(Chat.class);
if (chat.getReceiver().equals(myID) && chat.getSender().equals(userID) ||
chat.getReceiver().equals(userID) && chat.getSender().equals(myID)) {
mChats.add(chat);
}

mMessageAdapter = new MessageAdapter(MessageActivity.this, mChats, imageURL);
mRecyclerView.setAdapter(mMessageAdapter);
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

Chat.java

public class Chat {

private String sender;
private String receiver;
private String message;

public Chat(String sender, String receiver, String message) {
this.sender = sender;
this.receiver = receiver;
this.message = message;
}

public Chat() {
}

public String getSender() {
return sender;
}

public void setSender(String sender) {
this.sender = sender;
}

public String getReceiver() {
return receiver;
}

public void setReceiver(String receiver) {
this.receiver = receiver;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

}

最佳答案

问题在于数据库中的属性与 Chat Pojo 类中的字段不匹配。

数据库中的属性应与Pojo一样全部小写,以便您可以将类映射到数据库。

您的数据库也应该是这样的:

 Chats
randomId
message : "hi"
reciever: "peter"
sender : "jmb"

这样,randomid 将充当一个对象,并且在该对象内,您还拥有 Chats 类中的 3 个属性。

关于java - for 循环阻止我从数据库检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60746849/

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