我正在编写一个聊天应用程序,而我的用户可以互相发送文本/图片(保存在Firebase中)。我正在使用Glide将图片从Firebase上传到ImageView。图片正确上传;但是,这些文本将作为我的图片的占位符上传。
有关更多信息: 在短信中,通常您会看到对手的文字(包括文字和图像),并且会看到自己的文字(文字和图像)。但是,在这种情况下,我只能看到图像。应该包含单词的文本将替换为占位符图像。
代码
package com.example.realtimechat.chats
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.realtimechat.R
import com.example.realtimechat.common.Constants
import com.example.realtimechat.common.Extras
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.storage.FirebaseStorage
import java.text.SimpleDateFormat
class MessagesAdapter(private val context: Context,
private val messageList: List<MessageModel> = mutableListOf()): RecyclerView.Adapter<MessagesAdapter.MessageViewHolder>() {
private lateinit var mAuth: FirebaseAuth
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessagesAdapter.MessageViewHolder {
val view: View = LayoutInflater.from(context).inflate(R.layout.custom_message_layout, parent, false)
return MessageViewHolder(view)
}
override fun onBindViewHolder(holder: MessagesAdapter.MessageViewHolder, position: Int) {
val message: MessageModel = messageList[position]
mAuth = FirebaseAuth.getInstance()
val currentUserId = mAuth.currentUser!!.uid
val fromUserId = message.MessageFrom
val sfd: SimpleDateFormat = SimpleDateFormat("dd-MM-yyyy HH:mm")
val dateTime = sfd.format(message.MessageTime)
val splitString: Array<String> = dateTime.split(" ").toTypedArray()
val messageTime = splitString[1]
Log.i("message time:", messageTime)
if(fromUserId == currentUserId){
if(message.MessageType.equals(Constants.MESSAGE_TYPE_TEXT)){
holder.llSent.visibility = View.VISIBLE
holder.llSentImage.visibility = View.GONE
}else{
holder.llSent.visibility = View.GONE
holder.llSentImage.visibility = View.VISIBLE
}
holder.llReceived.visibility = View.GONE
holder.llReceivedImage.visibility = View.GONE
holder.tvSentMessage.text = message.Message
holder.tvSentMessageTime.text = messageTime
holder.tvImageSentTime.text = messageTime
Glide.with(context)
.load(message.Message)
.placeholder(R.drawable.ic_image)
.into(holder.ivSent)
}else{
if(message.MessageType.equals(Constants.MESSAGE_TYPE_TEXT)){
holder.llReceived.visibility = View.VISIBLE
holder.llReceivedImage.visibility = View.GONE
}else{
holder.llReceived.visibility = View.GONE
holder.llReceivedImage.visibility = View.VISIBLE
}
holder.llSent.visibility = View.GONE
holder.llSentImage.visibility = View.GONE
holder.tvReceivedMessage.text = message.Message
holder.tvReceivedMessageTime.text = messageTime
holder.tvImageReceivedTime.text = messageTime
Glide.with(context)
.load(message.Message)
.placeholder(R.drawable.ic_image)
.into(holder.ivReceived)
}
holder.clMessage.setTag(R.id.TAG_MESSAGE, message.Message)
holder.clMessage.setTag(R.id.TAG_MESSAGE_ID, message.Message)
holder.clMessage.setTag(R.id.TAG_MESSAGE_TYPE, message.Message)
holder.clMessage.setOnClickListener{view ->
val messageType: String = view.getTag(R.id.TAG_MESSAGE_TYPE).toString()
val uri: Uri = Uri.parse(view.getTag(R.id.TAG_MESSAGE_TYPE).toString())
if(messageType == Constants.MESSAGE_TYPE_VIDEO){
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.setDataAndType(uri, "video/mp4")
context.startActivity(intent)
}else if(messageType == Constants.MESSAGE_TYPE_IMAGE){
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.setDataAndType(uri, "image/jpg")
context.startActivity(intent)
}
}
}
override fun getItemCount(): Int {
return messageList.size
}
inner class MessageViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val llSent: LinearLayout = itemView.findViewById(R.id.llSent)
val llReceived: LinearLayout = itemView.findViewById(R.id.llReceived)
val llSentImage: LinearLayout = itemView.findViewById(R.id.llSentImage)
val llReceivedImage: LinearLayout = itemView.findViewById(R.id.llReceivedImage)
val tvSentMessage: TextView = itemView.findViewById(R.id.tvSentMessage)
val tvSentMessageTime: TextView = itemView.findViewById(R.id.tvSentMessageTime)
val tvReceivedMessage: TextView = itemView.findViewById(R.id.tvReceivedMessage)
val tvReceivedMessageTime: TextView = itemView.findViewById(R.id.tvReceivedMessageTime)
val ivSent: ImageView = itemView.findViewById(R.id.ivSent)
val ivReceived: ImageView = itemView.findViewById(R.id.ivReceived)
val tvImageSentTime: TextView = itemView.findViewById(R.id.tvSentImageTime)
val tvImageReceivedTime: TextView = itemView.findViewById(R.id.tvReceivedImageTime)
val clMessage: ConstraintLayout = itemView.findViewById(R.id.clMessage)
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/clMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/llSent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginEnd="8dp"
android:orientation="horizontal"
android:visibility="gone"
android:background="@drawable/sent_message_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible">
<TextView
android:id="@+id/tvSentMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:padding="12dp"
android:textColor="@android:color/white"
android:autoLink="all"
android:textColorLink="@android:color/holo_blue_bright"
tools:text="What's up man" />
<TextView
android:id="@+id/tvSentMessageTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="10:00"
android:layout_gravity="bottom"
android:textSize="12sp"
android:layout_marginEnd="4dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/llSentImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginEnd="8dp"
android:layout_marginTop="4dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/llSent"
android:visibility="gone"
tools:visibility="visible">
<ImageView
android:id="@+id/ivSent"
android:layout_width="90dp"
android:layout_height="100dp"
android:background="@drawable/sent_message_background"
android:src="@drawable/ic_image"
android:contentDescription="Sent Image" />
<TextView
android:id="@+id/tvSentImageTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="10:00"
android:layout_gravity="bottom|end"
android:textSize="12sp"
android:layout_marginEnd="4dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/llReceived"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginStart="8dp"
android:orientation="horizontal"
android:visibility="gone"
android:background="@drawable/received_message_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/llSent"
tools:visibility="visible">
<TextView
android:id="@+id/tvReceivedMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:padding="12dp"
android:autoLink="all"
android:textColorLink="@android:color/holo_blue_bright"
tools:text="What's up man" />
<TextView
android:id="@+id/tvReceivedMessageTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="10:00"
android:layout_gravity="bottom"
android:textSize="12sp"
android:layout_marginEnd="4dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/llReceivedImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/llReceived"
android:visibility="gone"
tools:visibility="visible">
<ImageView
android:id="@+id/ivReceived"
android:layout_width="90dp"
android:layout_height="100dp"
android:background="@drawable/received_message_background"
android:src="@drawable/ic_image"
android:contentDescription="Received Image"/>
<TextView
android:id="@+id/tvReceivedImageTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="10:00"
android:layout_gravity="bottom"
android:textSize="12sp"
android:layout_marginEnd="4dp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
问题 出现 :具体出现在OnBindViewHolder
功能中:
Glide.with(context)
.load(message.Message)
.placeholder(R.drawable.ic_image)
.into(holder.ivSent)
和
Glide.with(context)
.load(message.Message)
.placeholder(R.drawable.ic_image)
.into(holder.ivReceived)
我使用Logcat来了解更多详细信息。 message.Message
返回了正确的值,只是文本值正在从可绘制文件夹中转换为图像。
PS 。logcat错误消息告诉我找不到该文件。据我所知,这是因为在执行Glide.with(context).load(message.Message)
时,系统正在尝试查找文本的图像文件,该文件不可用,因为只有文本。 (如果我错了,请纠正我)
任何解决方案将不胜感激!
答案 0 :(得分:0)
尝试
Glide.with(holder.ivReceived)
.load(message.Message)
.placeholder(R.drawable.ic_image)
.into(holder.ivReceived);