AndroidX
AndroidX copied to clipboard
LiveData Type Parameters Error
Xamarin.Android Version (eg: 6.0): 10.2.0.100
Operating System & Version (eg: Mac OSX 10.11): Windows 10 Pro 1903
Support Libraries Version (eg: 23.3.0): Lifecycle.LiveData 2.2.0
Describe your Issue:
When I attempt to use the MutableLiveData object with a type parameter, it fails to build with the error message "The non-generic type 'MutableLiveData' cannot be used with type argument". However, MutableLiveData is noted with the generic type in the documentation (I've linked it below). Let me know if this is intentional as I'm rather new to Xamarin and Android. Thanks!
Steps to Reproduce (with link to sample solution if possible):
Create a ViewModel class using the following tutorial and build. https://developer.android.com/topic/libraries/architecture/livedata#create_livedata_objects
MutableLiveData Documentation: https://developer.android.com/reference/androidx/lifecycle/MutableLiveData
@AstroWedge
Thanks for the feedback.
Just short answer, before I dig into the matter: Java generics are completely different from C# (.NET CLR) generics and our tooling has some limitations and sometimes we resort to convert generic types Xamarin to divine Java.Lang.Object
.
I need to dive in and see if the tool did automatic conversion or I had to change something. Normally I try to find derived non-generic type that makes sense and if that does not help, then Java.Lang.Object
.
@AstroWedge
Can you provide minimal sample, so I can see what you want to achieve and whether this is possible. Othrwise I will need to close this issue, because our support for java generics is limited. Meaning not everything written in java can be ported 1:1 to c#.
@moljac
If it is of any help, here is a simple idea - in Java - of what I'm trying to achieve in Xamarin.Android.
There is also a Xamarin repository linked to in #77 in which this issue is experienced.
@AstroWedge have you tried to wrap your c# object inside java object and pass it to MutableLivedata? It is not the ideal solution, but works.
Hey @tpow4,
Did you find an answer to you question ?
I am trying to convert the following java to c#
public final class CameraXViewModel extends AndroidViewModel {
private static final String TAG = "CameraXViewModel";
private MutableLiveData<ProcessCameraProvider> cameraProviderLiveData;
/**
* Create an instance which interacts with the camera service via the given application context.
*/
public CameraXViewModel(@NonNull Application application) {
super(application);
}
public LiveData<ProcessCameraProvider> getProcessCameraProvider() {
if (cameraProviderLiveData == null) {
cameraProviderLiveData = new MutableLiveData<>();
ListenableFuture<ProcessCameraProvider> cameraProviderFuture =
ProcessCameraProvider.getInstance(getApplication());
cameraProviderFuture.addListener(
() -> {
try {
cameraProviderLiveData.setValue(cameraProviderFuture.get());
} catch (ExecutionException | InterruptedException e) {
// Handle any errors (including cancellation) here.
Log.e(TAG, "Unhandled exception", e);
}
},
ContextCompat.getMainExecutor(getApplication()));
}
return cameraProviderLiveData;
}
}
Same error
The solution is to omit the generics in C#, as C# bindings generally do not contain Java generics.