Unable to resolve reference BR at runtime databinding android - android

I am trying to use databinding with MVVM in my android app but it is giving me an error: "Unable to resolve reference BR" at runtime while i can see the class already exist in the project.
build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.abc.xxx"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
def lifecycle_version = "2.2.0"
def arch_version = "2.1.0"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
}
ViewModel class:
class SearchViewModel : BaseObservable() {
private var searchModel = SearchModel("")
fun setSearchText(text: String){
searchModel.text = text
// notifyPropertyChanged(B)
notifyPropertyChanged(BR._all)
}
#Bindable
fun getSearchText(): String{
return searchModel.text
}
fun onSearchClicked(){
if(searchModel.text.isNullOrEmpty())
setSearchText("Error!!!")
}
}
Activity class:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val bind : ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
bind.viewModel = SearchViewModel()
bind.executePendingBindings()
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.abc.xxx.viewmodels.SearchViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/inImageSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:maxLines="1"
android:text="#={viewModel.userText}" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="#{()-> viewModel.onSearchClicked()}"
android:text="#string/search"
/>
</LinearLayout>
</layout>
So what are the changes i should do to make it work? I have tried many SO threads already but none of them is working. Please help me out.

It appears you are binding to a property that doesn't exit. You have the userText in your xml which doesn't exist. Also you are binding to a model of the viewModel, so you need to make sure your SearchModel also implements BaseObservable.
Then make sure it is not private in your viewModel or that you write appropriate getters to retrieve it. The get and set Names MUST match the property name that is labeled with bindable.
Also just an architecture comment. I'm not sure why you would create a searchViewModel and a searchModel. Seems sort of redundant, but maybe you have reasons.
Example SEARCH MODEL below
class SearchModel : BaseObservable() {
#Bindable
private var searchText = ""
fun setSearchText(text: String){
searchModel.text = text
notifyPropertyChanged(BR.searchText)
}
fun getSearchText(): String{
return searchModel.text
}
}
Then your XML needs to be bound correctly to the models property
<EditText
android:id="#+id/inImageSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/search_hint"
android:maxLines="1"
android:text="#={viewModel.searchModel.searchText}" />

Related

onClick at XML can't see method from viewmodel using Room & databinding

It seems I have some problem with kapt. I study Android and try to create app with Room and Coroutines.
I get this kind of mistakes:
enter image description here
and my onclicklistener at XML is underlined with red, and the mistake here is "Cannot find identifier 'onSave' ".
I also tried to set onclick listener at the fragment, but I got the same mistakes.
Neither invalidating caches nor rebuilding project helps.
AddViewModel.kt:
`package com.example.memorizewords.add
import android.app.Application
import androidx.lifecycle.*
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
import androidx.lifecycle.viewmodel.CreationExtras
import com.example.memorizewords.database.Word
import com.example.memorizewords.database.WordDatabase
import com.example.memorizewords.database.WordDatabaseDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
class AddViewModel(
val database: WordDatabaseDao?,
application: Application) : AndroidViewModel(application) {
private val _targetWord = MutableLiveData<String>()
private val _nativeWord = MutableLiveData<String>()
private suspend fun insert(word: Word) {
database?.insert(word)
}
private fun onSave() {
runBlocking {
viewModelScope.launch(Dispatchers.IO) {}
// val newWord = Word(0L, _targetWord.value!!, _nativeWord.value!!, true)
val newWord = Word(0L, "String", "String", true)
insert(newWord)
}
}
companion object {
val Factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
#Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(
modelClass: Class<T>,
extras: CreationExtras
): T {
val application = checkNotNull(extras[APPLICATION_KEY])
val dataSource = WordDatabase.getInstance(application).WordDatabaseDao
AddViewModel(dataSource, application)
return AddViewModel(dataSource, application) as T
}
}
}
}`
AddFragment.kt:
`package com.example.memorizewords.add
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.View
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.example.memorizewords.R
import com.example.memorizewords.databinding.FragmentAddBinding
class AddFragment : Fragment() {
private val viewModel: AddViewModel by viewModels { AddViewModel.Factory }
lateinit var targetWord: String
lateinit var nativeWord: String
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val binding: FragmentAddBinding = DataBindingUtil.inflate(
inflater, R.layout.fragment_add, container, false)
binding.setLifecycleOwner(this)
binding.addViewModel = viewModel
}`
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">
<data>
<variable
name="addViewModel"
type="com.example.memorizewords.add.AddViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/targetBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/button_margin_left_right"
android:layout_marginEnd="#dimen/button_margin_left_right"
android:hint="#string/enterTarget"
android:textSize="#dimen/edit_text_size"
app:layout_constraintBottom_toTopOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.15" />
<EditText
android:id="#+id/nativeBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/button_margin_left_right"
android:layout_marginEnd="#dimen/button_margin_left_right"
android:hint="#string/enterNative"
android:textSize="#dimen/edit_text_size"
app:layout_constraintBottom_toTopOf="#+id/guideline3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/guideline2" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3" />
<Button
android:id="#+id/saveButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/save_button_margin"
android:layout_marginEnd="#dimen/save_button_margin"
android:text="#string/save"
android:textSize="#dimen/button_text_size"
android:onClick="#{() -> addViewModel.onSave()}" //onSave here is underlined with red
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/guideline3"
app:layout_constraintVertical_bias="0.1" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>`
module's build.gradle
`plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'androidx.navigation.safeargs.kotlin'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.memorizewords"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
dataBinding true
viewBinding true
}
}
dependencies {
// Android KTX
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.7.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
// ViewModel and LiveData
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
// Navigation
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
// Room and Lifecycle dependencies
implementation "androidx.room:room-runtime:2.4.3"
kapt "androidx.room:room-compiler:2.4.3"
// Kotlin Extensions and Coroutines support for Room
implementation("androidx.room:room-ktx:2.4.3")
// Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1"
// Testing
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
implementation 'com.jakewharton.timber:timber:4.7.1'
}`
app's build.gradle
`plugins {
id 'com.android.application' version '7.2.0' apply false
id 'com.android.library' version '7.2.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
id 'androidx.navigation.safeargs.kotlin' version '2.4.1' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}`

Cannot find identifier 'currentItem' in Kotlin using Android Studio 4.2

First of all, I'm new into kotlin and I found issues within my learning process. So I found these errors:
"Cannot find identifier 'Current Item"
The errors occured in the xml file. here is code of the xml file:
<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>
<variable
name="args"
type="com.example.todoapp.fragments.update.UpdateFragment" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
tools:context=".fragments.update.UpdateFragment">
<EditText
android:id="#+id/current_title_et"
android:layout_width="0dp"
android:layout_height="60dp"
android:background="#drawable/custom_input"
android:ems="10"
android:hint="#string/title"
android:inputType="textPersonName"
android:paddingStart="24dp"
android:paddingEnd="24dp"
android:text="#{args.currentItem.title}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="#+id/current_priorities_spinner"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginTop="8dp"
android:background="#drawable/custom_input"
android:entries="#array/priorities"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:parsePriorityToInt="#{args.currentItem.priority}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/current_title_et" />
<EditText
android:id="#+id/current_description_et"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:background="#drawable/custom_input"
android:ems="10"
android:gravity="top|start"
android:hint="#string/description"
android:inputType="textMultiLine"
android:paddingStart="24dp"
android:paddingTop="16dp"
android:paddingEnd="24dp"
android:text="#{args.currentItem.description}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/current_priorities_spinner" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
and this is the kt file
package com.example.todoapp.fragments.update
import android.app.AlertDialog
import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.example.todoapp.R
import com.example.todoapp.data.models.ToDoData
import com.example.todoapp.data.viewmodel.ToDoViewModel
import com.example.todoapp.databinding.FragmentUpdateBinding
import com.example.todoapp.fragments.SharedViewModel
class UpdateFragment : Fragment() {
private val args by navArgs<UpdateFragmentArgs>()
private val mSharedViewModel: SharedViewModel by viewModels()
private val mToDoViewModel: ToDoViewModel by viewModels()
private var _binding: FragmentUpdateBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Data binding
_binding = FragmentUpdateBinding.inflate(inflater, container, false)
// Set Menu
setHasOptionsMenu(true)
// Spinner Item Selected Listener
binding.currentPrioritiesSpinner.onItemSelectedListener = mSharedViewModel.listener
return binding.root
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.update_fragment_menu, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_save -> updateItem()
R.id.menu_delete -> confirmItemRemoval()
}
return super.onOptionsItemSelected(item)
}
private fun updateItem() {
val title = binding.currentTitleEt.text.toString()
val description = binding.currentDescriptionEt.text.toString()
val getPriority = binding.currentPrioritiesSpinner.selectedItem.toString()
val validation = mSharedViewModel.verifyDataFromUser(title, description)
if (validation) {
// Update Current Item
val updatedItem = ToDoData(
args.currentItem.id,
title,
mSharedViewModel.parsePriority(getPriority),
description
)
mToDoViewModel.updateData(updatedItem)
Toast.makeText(requireContext(), "Successfully updated!", Toast.LENGTH_SHORT).show()
// Navigate back
findNavController().navigate(R.id.action_updateFragment_to_listFragment)
} else {
Toast.makeText(requireContext(), "Please fill out all fields.", Toast.LENGTH_SHORT)
.show()
}
}
// Show AlertDialog to Confirm Item Removal
private fun confirmItemRemoval() {
val builder = AlertDialog.Builder(requireContext())
builder.setPositiveButton("Yes") { _, _ ->
mToDoViewModel.deleteItem(args.currentItem)
Toast.makeText(
requireContext(),
"Successfully Removed: ${args.currentItem.title}",
Toast.LENGTH_SHORT
).show()
findNavController().navigate(R.id.action_updateFragment_to_listFragment)
}
builder.setNegativeButton("No") { _, _ -> }
builder.setTitle("Delete '${args.currentItem.title}'?")
builder.setMessage("Are you sure you want to remove '${args.currentItem.title}'?")
builder.create().show()
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
this is the plugin and dependency
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: "kotlin-kapt"
apply plugin: "androidx.navigation.safeargs.kotlin"
apply plugin: "kotlin-parcelize"
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.todoapp"
minSdkVersion 26
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures{
dataBinding = true
viewBinding = true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// Navigation Component
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
// Room components
implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
implementation "androidx.room:room-ktx:2.3.0"
androidTestImplementation "androidx.room:room-testing:2.3.0"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-common-java8:2.3.1"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
// Kotlin components
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0-native-mt"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0-native-mt"
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.10"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Any help or explanation would be appreciated

databinding msg Could not find accessor within kotlin dataclass

Error message
Found data binding error(s):
[databinding] {"msg":"Could not find accessor com.dubhe.room.entity.User.name","file":"app\\src\\main\\res\\layout\\activity_add_user.xml","pos":[{"line0":31,"col0":28,"line1":31,"col1":36}]}
My layout 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">
<data>
<import
alias="user"
type="com.dubhe.room.entity.User" />
<variable
name="add"
type="android.view.View.OnClickListener" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="addUser" />
<EditText
android:id="#+id/editUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="name"
android:text="#{user.name}" /> <-error in this line.
<EditText
android:id="#+id/editUserAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="age"
android:inputType="number"
android:text="#{user.age}" />
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="#{add}"
android:text="Add" />
</LinearLayout>
</layout>
My entity is a kotlin data class.
#Entity(tableName = "user")
data class User(
#PrimaryKey(autoGenerate = true) #ColumnInfo(name = "user_id") var id: Int = 0,
#ColumnInfo(name = "user_name")var name: String = "",
#ColumnInfo(name = "user_age")var age: Int = 0,
#Ignore var isChecked: Boolean = false
)
build.gradle in app directory.
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.dubhe.databinding"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
dataBinding {
enabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.google.android.material:material:1.1.0-alpha08'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// implementation 'com.android.support:support-vector-drawable:29.0.0'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//Room
implementation 'android.arch.persistence.room:runtime:2.1.4'
//BaseRecyclerViewAdapter
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.46'
annotationProcessor 'android.arch.persistence.room:compiler:2.1.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
}
No matter if I clean and rebuild or Invalidate Caches, I can't compile.
You are using an import instead of a variable:
This:
<import alias="user" type="com.dubhe.room.entity.User" />
should be this:
<variable name="user" type="com.dubhe.room.entity.User" />

cannot find symbol import yodgorbekkomilov.edgar.footballapp.databinding.FootballItemBindingImpl

I am developing football statics app but when I run the code I am getting following exception
cannot find symbol
import yodgorbekkomilov.edgar.footballapp.databinding.FootballItemBindingImpl;
^
symbol: class FootballItemBindingImpl
below XML class
<?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">
<data>
<variable
name="viewModel"
type="yodgorbekkomilov.edgar.footballapp.ui.FootballViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:mutableText="#{viewModel.clubName}"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:mutableText="#{viewModel.countryName}"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:mutableText="#{viewModel.clubValue}"/>
<ImageView
app:imageUrl="#{viewModel.image}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:id="#+id/clubImage"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:mutableText="#{viewModel.europeanTitle}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
below my FootballAdapter.kt where I have used databinding
import android.os.Build
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.RequiresApi
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import yodgorbekkomilov.edgar.footballapp.FootballResponse
import yodgorbekkomilov.edgar.footballapp.R
import yodgorbekkomilov.edgar.footballapp.databinding.FootballItemBinding
import yodgorbekkomilov.edgar.footballapp.ui.FootballViewModel
class FootballAdapter :
RecyclerView.Adapter<FootballAdapter.ViewHolder>() {
private lateinit var footballList: List<FootballResponse>
fun updatePostList(footballList: List<FootballResponse>) {
this.footballList = footballList
notifyDataSetChanged()
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): FootballAdapter.ViewHolder {
val binding: FootballItemBinding = DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
R.layout.football_item,
parent,
false
)
return ViewHolder(binding)
}
override fun getItemCount(): Int {
return footballList.size
}
#RequiresApi(Build.VERSION_CODES.O)
override fun onBindViewHolder(holder: FootballAdapter.ViewHolder, position: Int) {
holder.bind(footballList[position])
}
class ViewHolder(private val binding: FootballItemBinding) : RecyclerView.ViewHolder(binding.root) {
private val viewModel = FootballViewModel()
fun bind(model: FootballResponse) {
viewModel.bind(model)
binding.viewModel = viewModel
}
}
}
below FootballViewModel.kt
import androidx.lifecycle.MutableLiveData
import yodgorbekkomilov.edgar.footballapp.FootballResponse
import yodgorbekkomilov.edgar.footballapp.base.BaseViewModel
class FootballViewModel: BaseViewModel() {
private val clubName = MutableLiveData<String>()
private val countryName = MutableLiveData<String>()
private val clubValue = MutableLiveData<String>()
private val clubImage = MutableLiveData<String>()
private val europeanTitle = MutableLiveData<String>()
fun bind(football: FootballResponse){
clubName.value= football[0].name
countryName.value = football[1].country
clubValue.value = football[2].value.toString()
clubImage.value = football[3].image
europeanTitle.value = football[4].europeanTitles.toString()
}
fun getClubName():MutableLiveData<String>{
return clubName
}
fun getCountryName():MutableLiveData<String>{
return countryName
}
fun getClubValue():MutableLiveData<String>{
return clubValue
}
fun getImage():MutableLiveData<String> {
return clubImage
}
fun getEuropeanTitle():MutableLiveData<String> {
return europeanTitle
}
}
below my app.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 29
buildToolsVersion "30.0.0"
defaultConfig {
applicationId "yodgorbekkomilov.edgar.footballapp"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures{
dataBinding = true
viewBinding = true
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
def room_version = "2.2.5"
def nav_version = "2.3.0"
//Navigation component
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
//RxJava
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'
implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
implementation "com.squareup.retrofit2:adapter-rxjava2:2.6.1"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
//Room
implementation "androidx.room:room-rxjava2:$room_version"
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
//Glide
implementation 'com.github.bumptech.glide:glide:4.11.0'
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.2'
implementation 'com.squareup.retrofit2:converter-gson:2.6.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.1.0'
//Dagger2
implementation 'com.google.dagger:dagger:2.27'
kapt 'com.google.dagger:dagger-compiler:2.27'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
what I have tried
1.invalidate cache restart
2. I have checked xml carefully it did not solve problem
3. tried following After migration to androidX: Databinding problem (Android Studio 4) and ActivityMainBindingImpl cannot be found in this one as well it did not solve my problem
I want to know where I am making mistake

Databingding V2 and kotlin,I can not find my BR?

Yesterday, I updated my Android Studio to 3.1. So I can only use Databinding V2.I can build successfully in Java.But fail in Kotlin.It told me Unresolved reference: BR
layout
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="mainViewModel"
type="com.example.shao.myapplication.ViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{mainViewModel.title}"/>
</LinearLayout>
</layout>
in Java
public class MainActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.setVariable(BR.mainViewModel, new ViewModel());
}
public static int getMainBr() {
return BR.mainViewModel;
}
}
in kotlin
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val mBinding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
mBinding.setVariable(BR.mainViewModel, ViewModel())
}
}
Android’s Data Binding with Kotlin
First of all, after having a created Android project in Android Studio, we need to add the Data Binding dependency and the ones of Kotlin to the build.gradle file of our app.
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
....
dataBinding {
enabled = true
}
}
dependencies {
...
// notice that the compiler version must be the same than our gradle version
kapt 'com.android.databinding:compiler:2.3.1'
}
First we need to create a model. In this case a basic one like User.kt
data class User(val name: String, val age: Int)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<variable
name="user"
type="com.kuma.sample.User"
/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.kuma.sample.MainActivity"
>
<TextView
android:id="#+id/user_name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="#{user.name}"
tools:text="Name"
/>
<TextView
android:id="#+id/user_age_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="#{Integer.toString(user.age)}"
tools:text="XX"
/>
</LinearLayout>
</layout>
Kotlin Class
package com.kuma.sample
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.kuma.kotlinsteps.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val user = User("Kuma", 23)
binding.setVariable(BR.user, user)
binding.executePendingBindings()
}
}
Solve Error Br
enter code hereapply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.example.icarusud.recyclerviewtest"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled true
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
//recyclerview
compile "com.android.support:recyclerview-v7:24.1.1"
//lastadapter
compile 'com.github.nitrico.lastadapter:lastadapter:1.1.1'
kapt "com.android.databinding:compiler:2.1.2"
}
repositories {
mavenCentral()
}
kapt { generateStubs = true }
Change your gradle setting according to below code try this code it helps you.
change
mBinding.setVariable(BR.mainViewModel, ViewModel())
to
mBinding.mainViewModel=ViewModel()
I worked around this issue by adding a BRUtils.java, then in the Kotlin files, accessing BR values in BRUtils.java
BRUtils.java:
/**
* since {#link BR} cannot be resolved in kotlin files during compile time
* for some reason, created this file to hold getters for {#link BR} fields
* that can then be accessed from said kotlin files.
*/
public class BRUtils {
private BRUtils() {}
public static int getMainViewModel() {
return BR.mainViewModel;
}
}
something.kt:
binding.setVariable(BRUtils.getMainViewModel(), ViewModel())

Categories

Resources