我正在使用this android tutorial修改我的简单Android应用程序以使用聊天功能。但我无法从Firebase实时数据库更新列表视图。我可以写入数据库,但无法读取数据并显示它。我尝试了不同的解决方案,但它根本不起作用。这是我搜索不同解决方案和修改项目后的最新代码。
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
public class ChatActivity extends AppCompatActivity {
FirebaseDatabase database = FirebaseDatabase.getInstance();
public DatabaseReference myRef = database.getReference("message");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
displayMessage();
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
displayMessage();
}
@Override
public void onCancelled(DatabaseError error) {
}
});
}
public void sendMessage(View v) {
EditText input = findViewById(R.id.input);
myRef.push()
.setValue(new ChatMessage(input.getText().toString(),
getUser())
);
input.setText("");
displayMessage();
}
private String getUser() throws NullPointerException {
return FirebaseAuth.getInstance()
.getCurrentUser()
.getDisplayName();
}
private void displayMessage() {
ListView listOfMessages = findViewById(R.id.list_of_messages);
FirebaseListAdapter<ChatMessage> adapter;
Query query = myRef;
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
.setQuery(query, ChatMessage.class)
.setLayout(R.layout.message)
.build();
adapter = new FirebaseListAdapter<ChatMessage>(options) {
@Override
protected void populateView(View v, ChatMessage model, int position) {
TextView messageText = v.findViewById(R.id.message_text);
TextView messageUser = v.findViewById(R.id.message_user);
TextView messageTime = v.findViewById(R.id.message_time);
messageText.setText(model.getMessageText());
messageUser.setText(model.getMessageUser());
messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",
model.getMessageTime()));
}
};
listOfMessages.setAdapter(adapter);
}
}
子消息的JSON文件是
{
"message" : {
"-L2hcJLFvPY0G1pjj-dY" : {
"messageText" : "Hi How are you? ",
"messageTime" : 1515815206276,
"messageUser" : ""
},
"-L2hcKsBwdQX-h_tSHpl" : {
"messageText" : "It is fine. ",
"messageTime" : 1515815212543,
"messageUser" : ""
},
"-L2hcMthlhbllagf9d8u" : {
"messageText" : "Some text for texting",
"messageTime" : 1515815220832,
"messageUser" : ""
}
}
}
以及ChatMessage.java文件代码
import java.util.Date;
public class ChatMessage {
private String messageText;
private String messageUser;
private long messageTime;
public ChatMessage(String messageText, String messageUser) {
this.messageText = messageText;
this.messageUser =messageUser;
messageTime = new Date().getTime();
}
public ChatMessage(){
}
public String getMessageText() {
return messageText;
}
public String getMessageUser() {
return messageUser;
}
public long getMessageTime() {
return messageTime;
}
}