我正在嘗試使用“this”為配接器提供背景關系
代碼是:
val shoeid = arrayOf(
"Nike air","Adidas","asics"
)
val progress = intArrayOf(
10,50,60
)
shoeArrayList = ArrayList()
for ( i in shoeid.indices ) {
val shoe = UserShoes(shoeid[i], progress[i])
shoeArrayList.add(shoe)
}
binding.listviewProfile.adapter = ShoeAdapter(this,shoeArrayList)
資料類:
data class UserShoes(var shoeName: String, var usage: Int)
配接器代碼:
class ShoeAdapter(private val context: Activity,private val arrayList: ArrayList<UserShoes>): ArrayAdapter<UserShoes>(context, R.layout.list_view,arrayList) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val inflater: LayoutInflater = LayoutInflater.from(context)
val view: View = inflater.inflate(R.layout.list_view, null)
val shoe: TextView = view.findViewById(R.id.shoeID)
val progress: ProgressBar = view.findViewById(R.id.progressID)
shoe.text = arrayList[position].shoeName
progress.progress = arrayList[position].usage
return super.getView(position, convertView, parent)
}
}
型別不匹配。必需:活動
uj5u.com熱心網友回復:
改變
class ShoeAdapter(private val context: Activity,...
至
class ShoeAdapter(private val context: Context,...
和
binding.listviewProfile.adapter = ShoeAdapter(this,shoeArrayList)
至
context?.run { binding.listviewProfile.adapter = ShoeAdapter(this,shoeArrayList) }
您也可以保留 Activity 但配接器不需要 Activity 因此最好使用更抽象的Context.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/493046.html
下一篇:活動未從意圖接收資料
