FirebaseListAdapter没有显示数据库结果android

时间:2018-01-15 22:01:38

标签: android firebase firebase-realtime-database firebaseui

我正在Android上制作一个聊天应用,它使用google firebase存储用户互相写入的消息。要向用户显示这些消息,我会从数据库中读取这些消息并将其整理到一个ListView的自定义ListAdapter

我正在使用com.firebaseui:firebase-ui:3.1.3

问题是列表视图始终为空,FirebaseListAdapter未显示数据库结果

Database image

private void displayChatMessages() {

    Query query = FirebaseDatabase.getInstance().getReference();
    FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
            .setQuery(query, ChatMessage.class)
            .setLayout(R.layout.community_message)
            .build();

    adapter = new FirebaseListAdapter<ChatMessage>(options){
        @Override
        protected void populateView(View v, ChatMessage model, int position) {
            TextView messageText = (TextView)v.findViewById(R.id.message_text);
            TextView messageUser = (TextView)v.findViewById(R.id.message_user);
            TextView messageTime = (TextView)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);

}

public class ChatMessage {

    private String messageText;
    private String messageUser;
    private long messageTime;

    public ChatMessage(String messageText, String messageUser) {
        this.messageText = messageText;
        this.messageUser = messageUser;

        // Initialize to current time
        messageTime = new Date().getTime();
    }

    public ChatMessage(){

    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public String getMessageUser() {
        return messageUser;
    }

    public void setMessageUser(String messageUser) {
        this.messageUser = messageUser;
    }

    public long getMessageTime() {
        return messageTime;
    }

    public void setMessageTime(long messageTime) {
        this.messageTime = messageTime;
    }
}

0 个答案:

没有答案