我正在尝试从 Backendless 获取数据,我正在使用 kotlin 和 MVVM 架构。我对 Kotlin 和 MVVM 都很陌生。这些日期将显示在片段(位于选项卡中)的 recyclerView 中。我收到此错误:
java.lang.IllegalArgumentException: Missing public, default no-argument constructor
at com.backendless.Persistence.of(Persistence.java:833)
at com.example.drake.kunuk.ui.buy.BuyViewModel.loadBuys(BuyViewModel.kt:26)
at com.example.drake.kunuk.ui.buy.BuyViewModel.getBuys(BuyViewModel.kt:19)
at com.example.drake.kunuk.ui.buy.BuyFragment.onActivityCreated(BuyFragment.kt:37)
..................................................... .....................
class BuyViewModel : ViewModel() {
// TODO: Implement the ViewModel
private lateinit var buys: MutableLiveData<List<Buy>>
fun getBuys(): LiveData<List<Buy>> {
if(!::buys.isInitialized) {
buys = MutableLiveData()
loadBuys()
}
return buys
}
private fun loadBuys(){
lateinit var buy: List<Buy>
Backendless.Persistence.of(Buy::class.java)
.find(object : AsyncCallback<List<Buy>> {
/*This will give the response from Backendless*/
override fun handleResponse(response: List<Buy>) {
for (i in response.indices) {
buy = listOf(response[i])
}
buys.value = buy
}
override fun handleFault(fault: BackendlessFault?) {
TODO("not implemented")
}
})
}
}
这是我的 BuyAdapter 的代码:
class BuyAdapter(internal var context: Context,
internal var ressource: Int,
internal var buyList: ArrayList<Buy>
) : RecyclerView.Adapter<BuyAdapter.ViewHolder>() {
override fun getItemCount(): Int {
return buyList.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context)
.inflate(ressource, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val buy: Buy = buyList[position]
holder.price.text = buy.price.toString()
}
class ViewHolder(v: View) : RecyclerView.ViewHolder(v){
var price: TextView = v.tvPrice
}