这是将数据从firebase加载到listView的方法。
尝试FirebaseListAdapter(this,R.layout.list_item,FirebaseDatabase.getInstance.getReference()......)
在此之前,它显示错误。
然后使用查询添加了FirebaseListOptions,如图所示.. 但是这段代码无法在listView中加载数据。
private void displayChatMessage() {
ListView listOfMessage = (ListView)findViewById(R.id.list_of_message);
Query query = FirebaseDatabase.getInstance().getReference().child("ChatMessage");
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
.setQuery(query, ChatMessage.class)
.build();
adapter = new FirebaseListAdapter<ChatMessage>(options){
@Override
protected void populateView(View v, ChatMessage model, int position) {
//Get references to the views of list_item.xml
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()));
}
};
listOfMessage.setAdapter(adapter);
}
}
要在Firebase中推送数据的代码段..
FirebaseDatabase.getInstance().getReference("ChatMessage").push().setValue(new ChatMessage(input.getText().toString(),FirebaseAuth.getInstance().getCurrentUser().getEmail()));