我的代码中有一些错误,没有解决。我已经为离线SMS应用程序创建了布局。我遵循了为在线SMS应用程序创建的代码。我对代码进行了更改,但在某些地方被卡住了。
我在下面粘贴了我的代码。我想创建类似该图像的布局:
我在Google和YouTube上进行了搜索,但它们都显示了在线SMS应用/聊天应用。
class MessageAdapter(val context: Context) : RecyclerView.Adapter<MessageAdapter.MessageViewHolder>(){
companion object {
const val VIEW_TYPE_MESSAGE_SENT = 1
const val VIEW_TYPE_MESSAGE_RECEIVED = 2
}
private val messages: ArrayList<Message> = ArrayList()
fun addMessage(message: Message){
messages.add(message)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder {
val view: View
if (viewType == VIEW_TYPE_MESSAGE_SENT) {
view = LayoutInflater.from(parent.context).inflate(R.layout.my_message, parent, false)
return MessageViewHolder(view)
} else {
view = LayoutInflater.from(parent.context).inflate(R.layout.other_message, parent, false)
return OtherMessageViewHolder(view)
}
}
override fun getItemCount(): Int {
return messages.size
}
override fun onBindViewHolder(holder: MessageViewHolder, position: Int) {
val message = messages.get(position)
holder.bind(message)
}
open inner class MessageViewHolder(view : View) : RecyclerView.ViewHolder(view){
private var messageText = view.txtMyMessage
private var timeText = view.txtMyMessageTime
open fun bind(message:Message){
messageText.text = message.message
timeText.text = DateUtils.fromMillisToTimeString(message.time)
}
}
inner class OtherMessageViewHolder (view: View) : MessageViewHolder(view) {
internal var messageText: TextView
internal var timeText: TextView
internal var nameText: TextView
internal var profileImage: ImageView
init {
messageText = itemView.text_message_body as TextView
timeText = itemView.text_message_time as TextView
nameText = itemView.text_message_name as TextView
profileImage = itemView.image_message_profile as ImageView
}
override fun bind(message: Message) {
messageText.text = message.message
nameText.text = message.user
timeText.text = DateUtils.fromMillisToTimeString(message.time)
enter code here: Utils.displayRoundImageFromUrl(context,message.getSender().getProfileUrl(), profileImage)
}
}
object DateUtils {
fun fromMillisToTimeString(millis: Long) : String {
val format = SimpleDateFormat("hh:mm a", Locale.getDefault())
return format.format(millis)
}
}
override fun getItemViewType(position: Int): Int {
val message = messages.get(position)
return if(App.user == message.user) {
VIEW_TYPE_MESSAGE_SENT
}
else {
VIEW_TYPE_MESSAGE_RECEIVED
}
}
}
预期:
1.In,"`view = LayoutInflater.from(parent.context).inflate(R.layout.my_message, parent, false)`
return MessageViewHolder(view)",`my_message` layout show.Similarly in else part too.
> 2. In,"`override fun bind(message: Message) {
messageText.text = message.message
nameText.text = message.user
timeText.text = DateUtils.fromMillisToTimeString(message.time)
}`" of `OtherMessageViewholder` ,`'message','user' and 'time'` show.Similarly in `MessageViewHolder` too.
>3.In "`Utils`",the image of the sender user show which we have saved since it is offline.
>4.In , "`return if(App.user == message.user) {
VIEW_TYPE_MESSAGE_SENT`" under `getItemViewType` function,replace it with some code that the app include sending and receiving layout since we do not use `LOG IN` feature , also it is offline.
实际结果:
1. `my_message` is highlighted as unresolved reference.
2. `'message','user' and 'time'` is highlighted as unresolved reference.
3. "`Utils` is highlighted as unresolved reference.
4. `"App"` and `"user"` are highlighted as unresolved reference.