[英]My layout file is not showing in setContent(R.layout.xml file)..?
I have written my code but in the code when I use view = ..(R.layout.my_message)
it shows error as such file does not exist while it is already created.我已经编写了我的代码,但是在代码中,当我使用
view = ..(R.layout.my_message)
它显示错误,因为这样的文件在已经创建时不存在。 While rewriting name again I got option of activity_list_item
which is not exist in my code.再次重写名称时,我得到了我的代码中不存在的
activity_list_item
选项。 What I do to get my layout xml file?我如何获取我的布局 xml 文件?
Project Structure项目结构
I have tried alot and search too but found nothing.我已经尝试了很多并进行了搜索,但一无所获。
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 = fromMillisToTimeString(millis = 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
}
fun bind(message: Message) {
messageText.text = message.message
timeText.text = fromMillisToTimeString(message.time)
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(message.isSending){
VIEW_TYPE_MESSAGE_SENT
} else {
VIEW_TYPE_MESSAGE_RECEIVED
}
}
}
Expected: (R.layout.my_message, parent, false) (R.layout.other_message, parent, false)
预期:
(R.layout.my_message, parent, false) (R.layout.other_message, parent, false)
Actual:实际的:
other_message
and my_message
are unresolved reference. other_message
和my_message
是未解析的引用。
if you'r sure, that the layouts you mentioned are in the res/layout folder, then maybe they just aren't recognized by the AndroidStudio.如果您确定您提到的布局在res/layout文件夹中,那么也许它们只是不被 AndroidStudio 识别。
so first try to clean / Rebuild you Project: in AndroidStudio (Mac) click on:所以首先尝试清理/重建你的项目:在AndroidStudio(Mac)中点击:
Build -> Clean Project
构建 -> 清理项目
then然后
Build -> Rebuilt Project
构建 -> 重建项目
if this doesn't help, then you have to clear the Cache and restart the Android Studio (Mac) :如果这没有帮助,那么您必须清除缓存并重新启动 Android Studio (Mac):
File -> Invalidate Caches / Restart...
文件 -> 使缓存无效/重新启动...
无效并重新启动缓存对我有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.