数据绑定+ Kotlin室。如何在Kotlin中绑定数据类?

时间:2019-03-18 06:03:30

标签: android kotlin android-room android-databinding

我已经制作了一个仅在kotlin中成功运行的Room数据库演示。但是现在我也将该演示转换为数据绑定。

问题是在kotlin中,我们将数据类作为会议室数据库的实体。在Java中,我们只制作一个pojo类。因此,这一切在Java中都很容易,但在kotlin中却有所不同。

我的问题和我的问题是如何在Kotlin中绑定数据类?

以下是我的数据类

@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
     }

适配器类

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

运行时错误。

  

e:[kapt]发生异常:android.databinding.tool.util.LoggedErrorException:找到了数据绑定   错误。

应用程序级别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'

}

1 个答案:

答案 0 :(得分:1)

以下代码对您有用:

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