问题描述
我在build.gradle中使用以下依赖项:
I'm using the following dependencies in build.gradle:
compile 'com.google.firebase:firebase-database:11.4.2'
compile 'com.firebaseui:firebase-ui-database:3.1.0'
compile 'com.firebaseui:firebase-ui-auth:3.1.0'
我的目标是用Firebase数据库中的数据填充 Listview
.我正在按照 Github Firebase 上的指南进行操作以下方法应将数据绑定到列表视图:
My goal is to populate a Listview
with data from a Firebase Database.I'm following the guide on Github FirebaseThe following method should bind the data to a listview:
private void showMessages() {
Query query = FirebaseDatabase.getInstance()
.getReference("notfication/unread/" + userMailAddress.replace(".", ","))
.orderByKey();
FirebaseListOptions<Message> options = new FirebaseListOptions.Builder<Message>()
.setLayout(R.layout.fbmessage_listitem)//Note: The guide doesn't mention this method, without it an exception is thrown that the layout has to be set.
.setQuery(query, Message.class)
.build();
FirebaseListAdapter<Message> adapter = new FirebaseListAdapter<Message>(options) {
@Override
protected void populateView(View v, Message model, int position) {
TextView tvMessage = v.findViewById(R.id.tv_message);
tvMessage.setText(model.getDateTimeCreated());
}
};
ListView readMessageList = findViewById(R.id.lvReadMessageList);
readMessageList.setAdapter(adapter);
}
这是包含TextViews的布局.
This is the layout that contains the TextViews.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:orientation="vertical">
<TextView
android:id="@+id/tv_created_on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:gravity="start"
android:paddingStart="5dp"
android:paddingEnd="0dp"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/tv_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:gravity="start"
android:paddingStart="5dp"
android:paddingEnd="0dp"
android:textColor="@android:color/black" />
</LinearLayout>
<ImageView
android:id="@+id/iv_message_read"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_weight="0.2"
android:contentDescription="@string/message_read"
android:elevation="2dp"
app:srcCompat="@drawable/android_ok_sign" />
<ImageView
android:id="@+id/iv_delete"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_weight="0.2"
android:contentDescription="@string/abc_delete"
android:elevation="2dp"
android:src="@drawable/ic_delete_red_48dp" />
模型:
public class Message {
private String message;
private String sender;
private String receiver;
private long timestamp;
private boolean isRead;
private String userMailAddress;
public Message() {
}
public Message(String message, String sender, String receiver, long timestamp, boolean isRead) {
this.message = message;
this.sender = sender;
this.receiver = receiver;
this.timestamp = timestamp;
this.isRead = isRead;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getSender() {
return sender;
}
public void setSender(String sender) {
this.sender = sender;
}
public String getReceiver() {
return receiver;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public boolean isRead() {
return isRead;
}
public void setRead(boolean read) {
isRead = read;
}
public String getUserMailAddress() {
return userMailAddress;
}
public void setUserMailAddress(String userMailAddress) {
this.userMailAddress = userMailAddress;
}
public String getDateTimeCreated() {
Calendar calendar = Calendar.getInstance(Locale.getDefault());
calendar.setTimeInMillis(timestamp);
String date = DateFormat.format("dd-MM-yyyy", calendar).toString();
String time = DateFormat.format("HH:mm", calendar).toString();
return date + "\r\n" + time;
}
}
无论我尝试执行什么操作,设置断点或记录TAG,都不会调用方法populateView将数据从模型绑定到文本字段.我在以前的FirebaseUI(仍在我的生产应用程序中)中使用了相同的布局,并且在那里一切正常.
No matter what i try, set a breakpoint or Log a TAG, the method populateView to bind the data from the model to the textfields isn't called.I use the same layouts in the previous FirebaseUI (still in my production app) and everything is working fine there.
有人知道为什么不调用覆盖方法populateView吗?
Does anyone know why the override method populateView isn't called?
推荐答案
我遇到了相同的问题,并使用以下方法解决了该问题:
I had the same problem, fixed it with:
@Override
protected void onStart() {
super.onStart();
mAdapter.startListening();
}
然后
@Override
protected void onStop() {
super.onStop();
mAdapter.stopListening();
}
与更新前一样::)
这篇关于Android使用FirebaseUI和FirebaseListOptions填充ListView不会调用populateView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!