ANAND PRABHAKAR
ANAND PRABHAKAR

Reputation: 11

MutableLiveData Object Creation in Xamarin Android

I am new to developing application in Xamarin Android. I integrated AndroidX.lifeCycle using nuggets. I know how to create an object private MutableLiveData currentName = new MutableLiveData(); in java Android. but difficult to create an object in Xamarin Android. Any one has idea how to use LiveData in Xamarin Android

Upvotes: 0

Views: 814

Answers (2)

jackduckworth
jackduckworth

Reputation: 1

I think the confusion is caused by the fact that the Xamarin API is referencing java objects.

When i created seprated class to implement IObserver then i had to implement alot of method. But noticed in your code you just override two methods. ?

That's because IObserver is a java interface. If you inherit from Java.Lang.Object then you just need to implement the one method:

    class MyObserver : Java.Lang.Object, IObserver
    {
        public void OnChanged (Java.Lang.Object o)
        {
        }
    }

Alternatively you could implement IObserver in an existing class that already inherits from Java.Lang.Object, e.g. your Adapter class.

Did you know how to covert C# object to java.lang.object ?

I think you're refering to the fact that the LiveData object, that is being observed, is a Java.Lang.Object.

In my case I wanted to observe a C# object, specifically a System.Collections.Generic.List. But this is not allowed as there is no way to convert between the System object and a Java object.

My solution was to use an Android.Runtime.JavaList object. This is a Java object and implements System.Collections.Generic.IList so I didn't have to go digging into the Java class hierarchy, I just used the C# IList methods.

IMHO the fact that we need to use java objects at all suggests that this is an area that the Xamarin team haven't prioritised.

Upvotes: 0

nevermore
nevermore

Reputation: 15816

You should install the Xamarin.Android.Arch.Lifecycle.LiveData nuget.Package first, right click your Xamarin.Android project and choose Manage Nuget Packages.., then search the package and install it as showed in below screenshot:

enter image description here

Then in your project, use the namespace and code :

using Android.Arch.Lifecycle;

MutableLiveData currentName = new MutableLiveData();

Update:

public class MainActivity : AppCompatActivity
{

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        MutableLiveData currentName = new MutableLiveData();
        currentName.SetValue("321");

        mylifeCycle lifeCycle = new mylifeCycle();
        mylObserver obsever = new mylObserver();

        currentName.Observe(lifeCycle, obsever);
    }
}


public class mylifeCycle : ILifecycleOwner
{
    public Lifecycle Lifecycle => throw new NotImplementedException();

    public IntPtr Handle => throw new NotImplementedException();

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

public class mylObserver : IObserver
{
    public IntPtr Handle => throw new NotImplementedException();

    public void Dispose()
    {
        throw new NotImplementedException();
    }

    public void OnChanged(Java.Lang.Object p0)
    {
        throw new NotImplementedException();
    }
}

Upvotes: 1

Related Questions