Pre:第一次声明我的viewHolder,最近使用Kotlin。也许我有一个非常基本的错误,但是我看不到它。
错误:
未解析的引用:ViewHolder">
在类声明中指定我的ViewHolder时发生,如下所示:
package gmn.suninja.recyclerviewproject
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Adapter
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
class Adapter: RecyclerView.Adapter<Adapter.ViewHolder>{ // ERROR here "ViewHolder"
// Atributes of Adapter: list of users.
private var userList: List<ModelClass>
// Constructor
constructor(userList: List<ModelClass> ){
this.userList = userList
}
/* Implement the three methods from interface. */
/* Inflates view and returns HeaderViewHolder. */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Adapter.ViewHolder {// ERROR here "ViewHolder"
val view: View = LayoutInflater.from(parent.context)
.inflate(R.layout.item_design, parent,false)
return ViewHolder(view)
}
/* To bind the data from the Main Activity or Data class inside the RecyclerView*/
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
var resource:Int = userList.get(position).getImageView()
var name: String = userList.get(position).getTextViewName()
var message: String = userList.get(position).getTextViewMessage()
var time:String = userList.get(position).getTextViewTime()
// Now, to send this data (above) to the holder
holder.setData(resource, name, message, time)
}
override fun getItemCount(): Int {
return userList.size
}
/* ViewHolder for displaying list. */
// #1 #custonRecyclerView -> create ViewHolder
class ViewHolder(val itemView: View): RecyclerView.ViewHolder(itemView) {
fun setData(resource: Int, name: String, message: String, time: String) {
ivImage.setImageResource(resource)
tvName.setText(name)
tvMessage.setText(message)
tvTime.setText(time)
}
private var ivImage: ImageView = itemView.findViewById(R.id.imageView)
private var tvName: TextView = itemView.findViewById(R.id.textViewName)
private var tvMessage: TextView = itemView.findViewById(R.id.textViewMessage)
private var tvTime: TextView = itemView.findViewById(R.id.textViewTime)
}
}
我尝试了几种方法。然而,错误发生在使用公共Android接口" " adapter . viewholder& quot;。所以,我不知道如何避免错误的发生,因为我必须实现它。
我是新手。再次感谢。
我已经找到并尝试了如何在Java中做到这一点,它是有效的。但不是在Kotlin中,所以这可能是一个非常基本的问题>_<</p>
与您的导入行冲突
import android.widget.Adapter
要么做
import android.widget.Adapter as AndroidAdapter
或者将您的类重命名为比"Adapter"更好的名称。
你基本上在类"Adapter"下声明了你的ViewHolder类,它与你从Android SDK导入的同名,所以它从Android的类中查找类ViewHolder
我希望这对你有帮助。:)
未解析的引用:ViewHolder">
告诉您正在引用ViewHolder
,并且无法看到该资源。你需要import
。您将需要import
必要的包(阅读此)
删除此导入import android.widget.Adapter
并从您的项目中导入适配器。