Sohel S9
Sohel S9

Reputation: 246

Databinding + Room in Kotlin. How to bind data class in Kotlin?

I have make one demo of Room database which is purely in kotlin and its successfully run. But now I convert that demo into databinding also.

Problem is that in kotlin we make data class as entity for room database. And in java we just make one pojo class. So its all about easy in Java but something different in kotlin.

My problem and my question is How to i bind data class in kotlin?

following is my data class

@Entity
data class Bill(

    @ColumnInfo(name = "billNo")
    var billNo: Int,
    @ColumnInfo(name = "customerName")
    var customerName: String,
    @ColumnInfo(name = "customerNo")
    var customerNo: String,
    @ColumnInfo(name = "itemList")
    @TypeConverters(ItemListConverter::class)
    var itemList: ArrayList<Item>
   ) {
       @PrimaryKey(autoGenerate = true)
      var billId: Int = 0
     }

Adapter Class

class BillAdapter(private val ctx: Context, private var payeeList: ArrayList<Bill>) : RecyclerView.Adapter<BillAdapter.ViewHolder>() {

var listdata = payeeList
var layoutInflater:LayoutInflater? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BillAdapter.ViewHolder {

    if(layoutInflater==null){
        layoutInflater = LayoutInflater.from(parent.getContext());
    }

var binding:BillListItemBinding =  DataBindingUtil.inflate(this!!.layoutInflater!!, R.layout.bill_list_item, parent, false)
    return ViewHolder(binding)
}

//this method is binding the data on the list
   override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding.bill =  listdata[position]

    }

//this method is giving the size of the list
override fun getItemCount(): Int {
    return listdata.size
}

   inner class ViewHolder(var binding: BillListItemBinding) : 
RecyclerView.ViewHolder(binding.root) {}
}

Error at run time.

e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.

app level build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'  // add this line

android {


    dataBinding {
        enabled = true
    }

    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.root.roomdatabinding"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    implementation 'io.reactivex.rxjava2:rxjava:2.1.17'
    implementation "android.arch.persistence.room:runtime:1.1.1"
    kapt "android.arch.persistence.room:compiler:1.1.1"
    implementation 'com.google.code.gson:gson:2.2.4'
    implementation 'android.arch.lifecycle:extensions:1.1.0'
    kapt 'com.android.databinding:compiler:3.1.1'

}

Upvotes: 1

Views: 1128

Answers (1)

Snehal Patil
Snehal Patil

Reputation: 11

Follow below code its useful for you:

class WeatherAdapter(val mContext: Context, private val guestLists:   List<WeatherData>) : RecyclerView.Adapter<WeatherAdapter.ViewHolder>() {
private var weatherList: WeatherData? = null



override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
    return ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.weather_row, viewGroup, false))
}

override fun onBindViewHolder(viewHolder: ViewHolder, i: Int) {
    weatherList = guestLists[i]



    viewHolder.bind(weatherList)

}

override fun getItemCount(): Int {
    return guestLists.size
}

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    private val mBinding: WeatherRowBinding?

    init {
        mBinding = DataBindingUtil.bind(itemView)
    }

    fun bind(weatherList: WeatherData?) {
        mBinding!!.setSetdata(weatherList)
    }
}
}

Upvotes: 1

Related Questions