在Kotlin中使用ViewModel时出错。
HCJob.kt
class HCJob {
private lateinit var jobImgURL: String
private lateinit var jobTitle: String
private lateinit var jobLocation: String
// Constructor
// Getters * Setters
}
HCYourJobViewModel.kt HCYourJobViewModel类:ViewModel {
private lateinit var job: HCJob
constructor() : super()
constructor(job: HCJob) : super() {
this.job = job
}
var yourJobListMutableLiveData = MutableLiveData<ArrayList<HCYourJobViewModel>>()
var yourJobList = ArrayList<HCYourJobViewModel>()
fun getYourJobList(): MutableLiveData<ArrayList<HCYourJobViewModel>> {
val job1 = HCJob("", "Director", "NewYork, NY")
val job2 = HCJob("", "Vice President", "Chicago")
val dashboardViewModel1: HCYourJobViewModel =
HCYourJobViewModel(job1)
val dashboardViewModel2: HCYourJobViewModel =
HCYourJobViewModel(job2)
yourJobList.add(dashboardViewModel1)
yourJobList.add(dashboardViewModel2)
yourJobListMutableLiveData.value = yourJobList
return yourJobListMutableLiveData
}
}
list_row_job.xml
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data class="HCYourJobBinding">
<import type="android.view.View" />
<variable
name="job_model"
type="com.hidden.client.ui.viewmodels.HCYourJobViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@+id/text_job_title"
android:text="@{job_model.job.jobTitle}"/>
<TextView
android:id="@+id/text_job_location"
android:text="@{job_model.job.jobLocation}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
HCYourJobVH.kt
class HCYourJobVH(private val jobBinding: HCYourJobBinding): RecyclerView.ViewHolder(jobBinding.root) {
fun bind (jobViewModel: HCYourJobViewModel) {
this.jobBinding.jobModel = jobViewModel
jobBinding.executePendingBindings()
}
}
DashboardFragment.kt
rvYourJobs = root.findViewById(R.id.recyclerview_your_jobs)
yourJobViewModel = ViewModelProviders.of(this).get(HCYourJobViewModel::class.java)
yourJobViewModel.getYourJobList().observe(this, Observer {yourJobViewModels->
yourJobAdapter = HCYourJobAdapter(activity!!.applicationContext, yourJobViewModels)
layoutYourJobManager = LinearLayoutManager(activity!!.applicationContext, LinearLayoutManager.HORIZONTAL, false)
rvYourJobs.layoutManager = layoutYourJobManager
rvYourJobs.setHasFixedSize(true)
rvYourJobs.adapter = yourJobAdapter
})
HCYourJobAdapter.kt
class HCYourJobAdapter(
private val context:Context,
private val list: ArrayList<HCYourJobViewModel>) : RecyclerView.Adapter<HCYourJobVH>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HCYourJobVH {
val layoutInflater = LayoutInflater.from(parent.context)
val jobBinding: HCYourJobBinding = DataBindingUtil.inflate(layoutInflater, R.layout.list_row_job, parent, false)
return HCYourJobVH(jobBinding)
}
override fun onBindViewHolder(holder: HCYourJobVH, position: Int) {
val jobModel = list[position]
holder.bind(jobModel)
holder.itemView.setOnClickListener(View.OnClickListener {
val intent = Intent(context, HCYourJobActivity::class.java)
HCGlobal.getInstance(context).g_currentActivity.startActivity(intent)
})
}
override fun getItemCount(): Int {
return list.size
}
}
我还在build.gradle中添加了一些代码
apply plugin: 'kotlin-kapt'
dataBinding {
enabled = true
}
kapt 'com.android.databinding:compiler:2.3.1'
运行应用程序时,出现以下错误
E:\ workspace \ hidden-client \ hidden-client-app-android \ app \ build \ Generated \ source \ kapt \ debug \ com \ hidden \ client \ DataBinderMapperImpl.java:9: 错误:找不到符号导入 com.hidden.client.databinding.HCYourJobBindingImpl; ^符号:类HCYourJobBindingImpl位置:包com.hidden.client.databinding
我已经在Google上进行搜索,并尝试了几种解决方案,但直到现在它都无法使用。 我该如何解决?