代码mRecyclerView.adapter= CustomAdapter(allList)
效果很好,我希望定义一个私有的var mCustomAdapter,然后分配值。
但是代码private lateinit var mCustomAdapter CustomAdapter
导致错误,我该如何解决?谢谢!
代码A
class UIMain : AppCompatActivity() {
private lateinit var mCustomAdapter CustomAdapter //Error
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_main)
...
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
mRecyclerView.adapter= CustomAdapter(allList) //OK
}
代码B
class CustomAdapter (val backupItemList: List<MSetting>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
private var mSelectedItem = -1
//this method is returning the view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_recyclerview, parent, false)
return ViewHolder(v)
}
fun getSelectedItem():Int{
return mSelectedItem
}
//this method is binding the data on the list
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(backupItemList[position])
holder.itemView.radioButton.setChecked(position == mSelectedItem);
}
//this method is giving the size of the list
override fun getItemCount(): Int {
return backupItemList.size
}
//the class is hodling the list view
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(aMSetting: MSetting) {
//itemView.radioButton.isChecked=false
itemView.radioButton.tag=aMSetting._id
itemView.textViewUsername.text=aMSetting.createdDate.toString()
itemView.textViewAddress.text=aMSetting.description
itemView.radioButton.setOnClickListener {
mSelectedItem=getAdapterPosition()
notifyDataSetChanged();
}
}
}
}
答案 0 :(得分:2)
MobileServicePushFailedException
变量
:
试试这个:
mCustomAdapter
查看更多:https://kotlinlang.org/docs/reference/basic-syntax.html#defining-variables