Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How do I get a Handle to a type.

RahRah USMember ✭✭

I have a Type which is ListItem and I need to return a Java.Lang.Object from an object of that type.

How do I get a valid IntPtr to execute the Java.Lang.Object.GetObject<Java.Lang.Object>(IntPtr handle, JniHandleOwnership transfer)?


  • JaredKellsJaredKells AUMember ✭✭

    If your type extends Java.Lang.Object the handle is in the Handle property.

    What are you trying to achieve? In most cases you don't need to touch the handle directly, you just need to subclass Java.Lang.Object.

  • RahRah USMember ✭✭

    I've created an application that runs on both windows and Android, the ListItem is an entity that is located in the core library. It can not extend Java.Lang.Object because my Windows UI will not understand this. So, I need to wrap the pointer to my ListItem in an Object so that my custom list adaptor can return it as a Java.Lang.Object.

    This is being done inside the standard ArrayAdaptor I use with my spinner, so I know it can be done but I need to know how.

    This works:
    private void spinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
    var spinner = (Spinner) sender;
    Object item = spinner.GetItemAtPosition(e.Position);
    var loginviewmodel = DataViewModel as LoginViewModel;
    loginviewmodel.SelectedUser = loginviewmodel.CurrentUser = item.Cast();
    string toast = string.Format("The selected user is {0}",
    Toast.MakeText(this, toast, ToastLength.Long).Show();

    But when I use my custom ItemListAdaptor it does not, because I am returning null from the GetItem override method when I should be returning my ListItem as a Java.Lang.Object.

Sign In or Register to comment.