Databinding with Kotlin and generics. error: incompatible types: Object cannot be converted to List
I bind a List
of Interfaces
with a type argument via databinding.
Interface:
public interface MyInterface<T> {
T getValue();
}
ViewModel:
public class MyViewModel {
public ObservableField<List<MyInterface>> name = new ObservableField<>();
}
BindingAdapter:
@android.databinding.BindingAdapter("bind")
public static void bind(TextView textView, List<MyInterface> list) {
}
XML:
<data>
<variable
name="viewModel"
type="com.example.myname.playground4.MyViewModel"/>
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:bind="@{viewModel.name}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
This works as long as the ViewModel
is in Java.
When I convert the ViewModel
to Kotlin:
class MyKotlinViewModel {
val name = ObservableField<List<MyInterface<*>>>()
}
I get an error in my ActivityMainBindingImpl.java
:
error: incompatible types: Object cannot be converted to List
Here's the faulty method:
@Override
protected void executeBindings() {
long dirtyFlags = 0;
synchronized(this) {
dirtyFlags = mDirtyFlags;
mDirtyFlags = 0;
}
android.databinding.ObservableField viewModelName = null;
java.util.List viewModelNameGet = null;
com.example.fweigl.playground4.MyKotlinViewModel viewModel = mViewModel;
if ((dirtyFlags & 0x7L) != 0) {
if (viewModel != null) {
// read viewModel.name
viewModelName = viewModel.getName();
}
updateRegistration(0, viewModelName);
if (viewModelName != null) {
// read viewModel.name.get()
viewModelNameGet = viewModelName.get(); // error is here
}
}
// batch finished
if ((dirtyFlags & 0x7L) != 0) {
// api target 1
com.example.fweigl.playground4.BindingAdapter.bind(this.mboundView0, viewModelNameGet);
}
}
Anyone know the reason for this and / or how to fix this?
You can try for yourself with my test project @ https://github.com/fmweigl/playground4. The (working) java version is on branch 'master', the (non-working) kotlin version on branch 'kotlin'.
android kotlin android-databinding
add a comment |
I bind a List
of Interfaces
with a type argument via databinding.
Interface:
public interface MyInterface<T> {
T getValue();
}
ViewModel:
public class MyViewModel {
public ObservableField<List<MyInterface>> name = new ObservableField<>();
}
BindingAdapter:
@android.databinding.BindingAdapter("bind")
public static void bind(TextView textView, List<MyInterface> list) {
}
XML:
<data>
<variable
name="viewModel"
type="com.example.myname.playground4.MyViewModel"/>
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:bind="@{viewModel.name}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
This works as long as the ViewModel
is in Java.
When I convert the ViewModel
to Kotlin:
class MyKotlinViewModel {
val name = ObservableField<List<MyInterface<*>>>()
}
I get an error in my ActivityMainBindingImpl.java
:
error: incompatible types: Object cannot be converted to List
Here's the faulty method:
@Override
protected void executeBindings() {
long dirtyFlags = 0;
synchronized(this) {
dirtyFlags = mDirtyFlags;
mDirtyFlags = 0;
}
android.databinding.ObservableField viewModelName = null;
java.util.List viewModelNameGet = null;
com.example.fweigl.playground4.MyKotlinViewModel viewModel = mViewModel;
if ((dirtyFlags & 0x7L) != 0) {
if (viewModel != null) {
// read viewModel.name
viewModelName = viewModel.getName();
}
updateRegistration(0, viewModelName);
if (viewModelName != null) {
// read viewModel.name.get()
viewModelNameGet = viewModelName.get(); // error is here
}
}
// batch finished
if ((dirtyFlags & 0x7L) != 0) {
// api target 1
com.example.fweigl.playground4.BindingAdapter.bind(this.mboundView0, viewModelNameGet);
}
}
Anyone know the reason for this and / or how to fix this?
You can try for yourself with my test project @ https://github.com/fmweigl/playground4. The (working) java version is on branch 'master', the (non-working) kotlin version on branch 'kotlin'.
android kotlin android-databinding
1
This sounds like something that should be a bug filed against Data Binding.
– ianhanniballake
Nov 23 '18 at 19:33
@ianhanniballake Will do, thanks.
– asco
Nov 24 '18 at 13:25
add a comment |
I bind a List
of Interfaces
with a type argument via databinding.
Interface:
public interface MyInterface<T> {
T getValue();
}
ViewModel:
public class MyViewModel {
public ObservableField<List<MyInterface>> name = new ObservableField<>();
}
BindingAdapter:
@android.databinding.BindingAdapter("bind")
public static void bind(TextView textView, List<MyInterface> list) {
}
XML:
<data>
<variable
name="viewModel"
type="com.example.myname.playground4.MyViewModel"/>
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:bind="@{viewModel.name}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
This works as long as the ViewModel
is in Java.
When I convert the ViewModel
to Kotlin:
class MyKotlinViewModel {
val name = ObservableField<List<MyInterface<*>>>()
}
I get an error in my ActivityMainBindingImpl.java
:
error: incompatible types: Object cannot be converted to List
Here's the faulty method:
@Override
protected void executeBindings() {
long dirtyFlags = 0;
synchronized(this) {
dirtyFlags = mDirtyFlags;
mDirtyFlags = 0;
}
android.databinding.ObservableField viewModelName = null;
java.util.List viewModelNameGet = null;
com.example.fweigl.playground4.MyKotlinViewModel viewModel = mViewModel;
if ((dirtyFlags & 0x7L) != 0) {
if (viewModel != null) {
// read viewModel.name
viewModelName = viewModel.getName();
}
updateRegistration(0, viewModelName);
if (viewModelName != null) {
// read viewModel.name.get()
viewModelNameGet = viewModelName.get(); // error is here
}
}
// batch finished
if ((dirtyFlags & 0x7L) != 0) {
// api target 1
com.example.fweigl.playground4.BindingAdapter.bind(this.mboundView0, viewModelNameGet);
}
}
Anyone know the reason for this and / or how to fix this?
You can try for yourself with my test project @ https://github.com/fmweigl/playground4. The (working) java version is on branch 'master', the (non-working) kotlin version on branch 'kotlin'.
android kotlin android-databinding
I bind a List
of Interfaces
with a type argument via databinding.
Interface:
public interface MyInterface<T> {
T getValue();
}
ViewModel:
public class MyViewModel {
public ObservableField<List<MyInterface>> name = new ObservableField<>();
}
BindingAdapter:
@android.databinding.BindingAdapter("bind")
public static void bind(TextView textView, List<MyInterface> list) {
}
XML:
<data>
<variable
name="viewModel"
type="com.example.myname.playground4.MyViewModel"/>
</data>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:bind="@{viewModel.name}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
This works as long as the ViewModel
is in Java.
When I convert the ViewModel
to Kotlin:
class MyKotlinViewModel {
val name = ObservableField<List<MyInterface<*>>>()
}
I get an error in my ActivityMainBindingImpl.java
:
error: incompatible types: Object cannot be converted to List
Here's the faulty method:
@Override
protected void executeBindings() {
long dirtyFlags = 0;
synchronized(this) {
dirtyFlags = mDirtyFlags;
mDirtyFlags = 0;
}
android.databinding.ObservableField viewModelName = null;
java.util.List viewModelNameGet = null;
com.example.fweigl.playground4.MyKotlinViewModel viewModel = mViewModel;
if ((dirtyFlags & 0x7L) != 0) {
if (viewModel != null) {
// read viewModel.name
viewModelName = viewModel.getName();
}
updateRegistration(0, viewModelName);
if (viewModelName != null) {
// read viewModel.name.get()
viewModelNameGet = viewModelName.get(); // error is here
}
}
// batch finished
if ((dirtyFlags & 0x7L) != 0) {
// api target 1
com.example.fweigl.playground4.BindingAdapter.bind(this.mboundView0, viewModelNameGet);
}
}
Anyone know the reason for this and / or how to fix this?
You can try for yourself with my test project @ https://github.com/fmweigl/playground4. The (working) java version is on branch 'master', the (non-working) kotlin version on branch 'kotlin'.
android kotlin android-databinding
android kotlin android-databinding
edited Nov 25 '18 at 14:37
Jayson Minard
40k17112173
40k17112173
asked Nov 23 '18 at 17:09
ascoasco
11.9k1679158
11.9k1679158
1
This sounds like something that should be a bug filed against Data Binding.
– ianhanniballake
Nov 23 '18 at 19:33
@ianhanniballake Will do, thanks.
– asco
Nov 24 '18 at 13:25
add a comment |
1
This sounds like something that should be a bug filed against Data Binding.
– ianhanniballake
Nov 23 '18 at 19:33
@ianhanniballake Will do, thanks.
– asco
Nov 24 '18 at 13:25
1
1
This sounds like something that should be a bug filed against Data Binding.
– ianhanniballake
Nov 23 '18 at 19:33
This sounds like something that should be a bug filed against Data Binding.
– ianhanniballake
Nov 23 '18 at 19:33
@ianhanniballake Will do, thanks.
– asco
Nov 24 '18 at 13:25
@ianhanniballake Will do, thanks.
– asco
Nov 24 '18 at 13:25
add a comment |
1 Answer
1
active
oldest
votes
I think that your problem is about your BindingAdapter.In Kotlin you have to add @JvmStatic
annotation to top of your BindingAdapter.
Like this:
@JvmStatic
@BindingAdapter("bind")
fun bind(recyclerView: RecyclerView, items: MutableList<SmartResult>) {
//Anything that you wanna do ...
}
Because when your ViewModel
an XML
want to use of your binding adapter it has to be static like in java!
1
The BindingAdapter is in Java and static and works with a Java ViewModel. Good idea though!
– asco
Nov 26 '18 at 22:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450641%2fdatabinding-with-kotlin-and-generics-error-incompatible-types-object-cannot-b%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think that your problem is about your BindingAdapter.In Kotlin you have to add @JvmStatic
annotation to top of your BindingAdapter.
Like this:
@JvmStatic
@BindingAdapter("bind")
fun bind(recyclerView: RecyclerView, items: MutableList<SmartResult>) {
//Anything that you wanna do ...
}
Because when your ViewModel
an XML
want to use of your binding adapter it has to be static like in java!
1
The BindingAdapter is in Java and static and works with a Java ViewModel. Good idea though!
– asco
Nov 26 '18 at 22:30
add a comment |
I think that your problem is about your BindingAdapter.In Kotlin you have to add @JvmStatic
annotation to top of your BindingAdapter.
Like this:
@JvmStatic
@BindingAdapter("bind")
fun bind(recyclerView: RecyclerView, items: MutableList<SmartResult>) {
//Anything that you wanna do ...
}
Because when your ViewModel
an XML
want to use of your binding adapter it has to be static like in java!
1
The BindingAdapter is in Java and static and works with a Java ViewModel. Good idea though!
– asco
Nov 26 '18 at 22:30
add a comment |
I think that your problem is about your BindingAdapter.In Kotlin you have to add @JvmStatic
annotation to top of your BindingAdapter.
Like this:
@JvmStatic
@BindingAdapter("bind")
fun bind(recyclerView: RecyclerView, items: MutableList<SmartResult>) {
//Anything that you wanna do ...
}
Because when your ViewModel
an XML
want to use of your binding adapter it has to be static like in java!
I think that your problem is about your BindingAdapter.In Kotlin you have to add @JvmStatic
annotation to top of your BindingAdapter.
Like this:
@JvmStatic
@BindingAdapter("bind")
fun bind(recyclerView: RecyclerView, items: MutableList<SmartResult>) {
//Anything that you wanna do ...
}
Because when your ViewModel
an XML
want to use of your binding adapter it has to be static like in java!
answered Nov 26 '18 at 13:59
EhsanEhsan
531727
531727
1
The BindingAdapter is in Java and static and works with a Java ViewModel. Good idea though!
– asco
Nov 26 '18 at 22:30
add a comment |
1
The BindingAdapter is in Java and static and works with a Java ViewModel. Good idea though!
– asco
Nov 26 '18 at 22:30
1
1
The BindingAdapter is in Java and static and works with a Java ViewModel. Good idea though!
– asco
Nov 26 '18 at 22:30
The BindingAdapter is in Java and static and works with a Java ViewModel. Good idea though!
– asco
Nov 26 '18 at 22:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450641%2fdatabinding-with-kotlin-and-generics-error-incompatible-types-object-cannot-b%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
This sounds like something that should be a bug filed against Data Binding.
– ianhanniballake
Nov 23 '18 at 19:33
@ianhanniballake Will do, thanks.
– asco
Nov 24 '18 at 13:25