在onCreate()中,我将其称为displayChatMessage() ListView什么也没提。 但是Firebase数据库中有数据 数据未显示在模拟器上。
private void displayChatMessage() {
ListView listOfMessage = findViewById(R.id.list_of_message);
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
.setQuery(FirebaseDatabase.getInstance().getReference(), ChatMessage.class)
.setLayout(R.layout.list_item)
.build();
adapter = new FirebaseListAdapter<ChatMessage>(options) {
@Override
protected void populateView(@NonNull View v, @NonNull ChatMessage model, int position) {
TextView messageText, messageUser, messageTime;
messageText = (BubbleTextView) 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);
}
我已将读写规则启用为true。 我的列表视图的代码如下- list_item.xml文件->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/message_user"
android:textStyle="normal|bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_alignParentBottom="@+id/message_user"
android:layout_alignParentEnd="true"
android:id="@+id/message_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.github.library.bubbleview.BubbleTextView
android:layout_alignParentStart="true"
android:layout_marginTop="5dp"
android:layout_below="@id/message_user"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#FFF"
android:textSize="18sp"
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
app:arrowWidth="8dp"
app:angle="8dp"
app:arrowHeight="10dp"
app:arrowPosition="14dp"
app:arrowLocation="left"
app:bubbleColor="#333639"
app:arrowCenter="true"
/>
</RelativeLayout>
答案 0 :(得分:0)
您必须传递一个查询,而不是像这样在FirebaseListOptions的setQuery方法中传递数据库引用,
Query query = FirebaseDatabase.getInstance().getReference().child("your node value");
然后像这样传递此查询
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
.setQuery(query, ChatMessage.class)
.setLayout(R.layout.list_item)
.build();
也不要忘记在onStart中启动startListen和在onStop中进行stopListen,如下所示:
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}