0

I'm trying to get data from a Backendless, I am using kotlin and MVVM arch. I'm very new to both Kotlin and MVVM. These date would be displayed in a recyclerView in a fragment(which is in a tab). I get this error:

 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")
            }

        })

}
}

This is the code of my 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

}
Dr4ke the b4dass
  • 1,184
  • 2
  • 17
  • 40

1 Answers1

2

The problem arises because there wasn't any no-argument constructor in the data class. So I changed this:

data class Buy(val address: String,
           val price: Int,
           val propertyDesc: String,
           val numberOfRoom: Int,
           val numberOfBath: Int,
           val numberOfCar: Int,
           val propertyImage: String,
           val propertyLocation: String,
           val amenities: String)

to that:

data class Buy(var address: String = "",
           var price: Int = 0,
           var propertyDesc: String = "",
           var numberOfRoom: Int = 0,
           var numberOfBath: Int = 0,
           var numberOfCar: Int = 0,
           var propertyImage: String = "",
           var propertyLocation: String = "",
           var amenities: String = "")

To have the compiler automatically generate a no-argument constructor, You have to assign a default value to each primary constructor parameter, and remember use var. Or You can declare a second constructor with no parameters.

Dr4ke the b4dass
  • 1,184
  • 2
  • 17
  • 40