使用firebaselistadpater在列表视图中未显示我的聊天记录

时间:2019-12-16 05:53:18

标签: java android firebase

这是我的显示聊天功能,它没有显示任何数据,也没有显示nay错误,请解释什么是错误或错误

private void displayChatMessage() {
ListView l = (ListView) findViewById(R.id.list_of_messages);
Query query = FirebaseDatabase.getInstance().getReference().child("Chat");
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage> 
().setQuery(query, ChatMessage.class).setLayout(R.layout.list_item).build();
FirebaseListAdapter<ChatMessage> adapter= new FirebaseListAdapter<ChatMessage>(options) {
@Override
protected void populateView (View v, ChatMessage model,int position){
TextView messageText, messageUser, messageTime;
messageText = (TextView) v.findViewById(R.id.message_text);
messageUser = (TextView) v.findViewById(R.id.message_user);
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()));
}
};
l.setAdapter(adapter);
}
}

1 个答案:

答案 0 :(得分:0)

您需要使用startListening()才能开始从Firebase获取数据并显示数据:

  

要开始填充数据,请调用startListening()方法。您可能想在onStart()方法中调用它。在调用startListening()之前,请确保已完成读取数据所需的所有身份验证,否则查询将失败。

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md