我有一个用于 Firebase 聊天的标准聊天显示方法:
private void displayChat() {
final ListView listOfMessage = findViewById(R.id.list_of_message);
final String DATABASE_URL = "https://firebasestorage.googleapis.com";
Query query = FirebaseDatabase.getInstance().getReference().child("messages").child(chatName).limitToLast(20);
FirebaseListOptions<Message> options = new FirebaseListOptions.Builder<Message>()
.setLayout(R.layout.list_item)
.setQuery(query, Message.class)
.setLifecycleOwner(this)
.build();
FirebaseListAdapter<Message> adapter = new FirebaseListAdapter<Message>(options) {
@Override
protected void populateView(View v, Message model, int position) {
//Get reference to the views of list_item.xml
TextView messageText, messageUser, messageTime;
ImageView img;
messageText = v.findViewById(R.id.message_text);
messageUser = v.findViewById(R.id.message_user);
messageTime = v.findViewById(R.id.message_time);
img = v.findViewById(R.id.imgView);
聊天功能齐全。但是,当涉及到图像时,我必须DATABASE_URL确定接收到的应该是图像而不是文本,因此我将其加载到 Picasso 中,如下所示:
if (model.getMessageText().contains(DATABASE_URL)) {
messageText.setText("Image sent: ");
Picasso.with(getApplicationContext()).load(model.getMessageText()).into(img);
}
但是,当它是普通文本时,我的:
else {
messageText.setText(model.getMessageText());
}
将正常显示文本,但仍显示图像 - 始终显示最近的图像。我曾尝试添加img.setVisibility(View.INVISIBLE);到else语句中,以及从 Glide/Picasso 中进行等效删除,但它们最终都会导致每张图像都被删除而不显示。
精慕HU
相关分类