I am trying to databind a viewmodel using the example project android-sunflower. The current issue is that when I am trying to build the project I get the error error: cannot find symbol symbol: class FragmentShopBindingImpl
location: package {{packageName}}.databinding
in the class DataBindinMapperImpl
I'm not really sure what I am missing here, since I added everything from the example project. The class FragmentShopBindingImpl
does not get generated, or shouldn't it? Since I cannot see any occurence of a class ending with 'Impl' in the android sunflower example
My code:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val factory = InjectorUtils.provideShopViewModelFactory(context!!)
val shopViewModel = ViewModelProviders.of(this, factory)
.get(ShopViewModel::class.java)
val binding = DataBindingUtil.inflate<FragmentShopBinding>(
inflater, R.layout.fragment_shop, container, false).apply {
viewModel = shopViewModel
lifecycleOwner = this@ShopFragment
}
return binding.root
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="{{packageName}}.viewmodel.ShopViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".fragments.ShopFragment">
<TextView
android:text="@{viewModel}"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</layout>
Image of generated file (ignore the {{packageName}}:
It seems the only thing I had to add was <import type="android.view.View" />
in the data tags...
In your xml code inside textView tag, for android:text attribute you have used @{viewmodel}. It just refers your shopViewModel class, you must target the text variable inside that class. Then the gen. class file errors will vanish.
bindingImpl errors are mostly generated for invalid assignment for XML-text or XML-onClick attributes.
If you use two-ways databinding (@={myBindingValue}
, with the '=' sign instead of @{myBindingValue}
) sometimes, you'll have this unusefull generic error because the value you are trying to bind is declared as immutable => val instead of var in Kotlin in your data class.
Exemple :
data class User(
val name,
var email
)
In this example, you could bind the user's email variable as : text="@={myViewModel.user.email}"
But, if you try to bind the user's name : text="@={myViewModel.user.name}"
you will get this error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With