我尝试使用Firebase进行聊天活动。我遇到了以下错误。任何帮助赞赏。 这也是我的第一篇文章。如果我没有在正确的部分发布这个,我道歉。
错误
Process: com.synthetech.synergyeventmanager, PID: 650
com.google.firebase.database.DatabaseException: Can't convert
object of type java.lang.String to type
com.synthetech.synergyeventmanager.MessageTemplate
CODE
ChatActivity.java
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Event/" + event_uid + "/Chat");
FirebaseListAdapter<MessageTemplate> firebaseListAdapter = new FirebaseListAdapter<MessageTemplate>(
this,
MessageTemplate.class,
R.layout.message_template,
databaseReference.orderByKey()) {
@Override
protected void populateView(View v, MessageTemplate msg, int position) {
TextView message = (TextView) findViewById(R.id.chatMessage);
TextView user = (TextView) findViewById(R.id.chatUser);
String chatMessageFetcher;
String chatUserFetcher;
chatMessageFetcher = msg.getMessage();
chatUserFetcher = msg.getUser();
message.setText(chatMessageFetcher);
user.setText(chatUserFetcher);
}
};
chatList.setAdapter(firebaseListAdapter);
MessageTemplate.java
public class MessageTemplate {
String message;
String user;
public MessageTemplate() {
}
public MessageTemplate(String message, String user) {
this.message = message;
this.user = user;
}
public String getMessage() {
return message;
}
public String getUser() {
return user;
}
}
点按推送消息的方法
@Override
public void onClick(View v) {
if (v == sendButton){
String event_uid;
String chat;
event_uid = eventuid_txt.getText().toString();
chat = message.getText().toString();
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
MessageTemplate msg = new MessageTemplate(chat,
firebaseAuth.getCurrentUser().getEmail());
DatabaseReference chatRef= FirebaseDatabase.getInstance().getReference().child("Event/"+event_uid+"/Chat");
chatRef.push().setValue(msg);
message.setText("");
}
}
JSON文件结构
{
"Event" : {
"Aseem_SDp3G2wfbBOGXsfPFWQBfVvj7wo2" : {
"Chat" : {
"
"message" : "Hey there! ",
"time" : 1492502099191,
"user" : "aseem@synergy.io"
}
}
}
请注意:我已更改了代码。你在那里看到的消息是之前添加的。我现在添加了 .push()方法。因此,一旦代码启动并运行,结构就会改变。 目前的结构将是这样的 -
Event
-Event_uid
-Chat
-Chat_uid
-message
-time
-user