开发者

convert Java.Lang.Object to .net type

I'm trying to cast a spinner.SelectedItem to string in monodroid. Since SelectedItem is a Java.Lang.Object, I need to know how can I cast a Java.Lang.Object to a native .net type. 开发者_JAVA百科Thank you, Alex


You didn't provide any context into what you're binding to the spinner, but I'm assuming it's just a list of strings? If that's the case, one possibility is to just do

spinner.SelectedItem.ToString()

That won't be very useful if the underlying object is not a string, though. You have some other options available as well. You can use spinner.SelectedItemPosition to get the item out of the spinner adapter's source. Something like:

var source = new List<string> { "a", "b", "c" };
spinner.Adapter = new ArrayAdapter<string>(this, Resource.Layout.Item, Resource.Id.Name, source);
var selectedString = source[spinner.SelectedItemPosition];

You could also do:

var selectedString = spinner.GetItemAtPosition[spinner.SelectedItemPosition].ToString();

It all depends on what you're trying to do and when. Xamarin has a tutorial up here that goes through the basics of using a spinner.


http://docs.xamarin.com/android/advanced_topics/architecture

This is a link to Xaramin's documentation for Mono's runtime architecture, which doesn't not directly answer the java to .net "casting" issue, it is certainly a starting point (and is where I stand myself in searching and producing solution).

I'll post anything useful if/when I come up with something.


I have a similar problem; particularly because I have a DataLayer PCL which I want to share between the various platforms. And I don't want to transform my DataLayer objects each time into a Java object, or an IOS object etc...

So, for the Android solution, this Wrapper/Decorator pattern has worked nicely;

  1. Imagine you have your PCL object

    public class GoldList
    {
        public string UniqueId { get; set; }
        public string Name { get; set; }
        public string Owner { get; set; }
    
        public GoldList()
        {
        }
    }
    
  2. Create an IJavaWrapper interface and JavaWrapper object which inherits from Java.Lang.Object:

    public abstract class JavaWrapper<T> : Java.Lang.Object, IJavaWrapper
    {
        private readonly T _item;
    
        protected JavaWrapper(T item)
        {
            _item = item;
        }
    
        public T Item
        {
            get { return _item; }
        }
    
        object IJavaWrapper.Item
        {
            get { return _item; }
        }
    }
    
    public interface IJavaWrapper
    {
        object Item { get; }
    }
    
  3. Create a concrete instance of this; wrapping your data object

    class GoldListJO : JavaWrapper<GoldList>
    {
        public GoldListJO() : this(new GoldList())
        {
        }
    
        public GoldListJO(GoldList item) : base(item)
        {
        }
    }
    
  4. Bind to the items (using example code):

    List<GoldListJO> items = new List<GoldListJO>();
    items.Add(new GoldListJO(new GoldList { Name = "1", UniqueId = "U1", Owner = "O1" }));
    items.Add(new GoldListJO(new GoldList { Name = "2", UniqueId = "U2", Owner = "O2" }));
    items.Add(new GoldListJO(new GoldList { Name = "3", UniqueId = "U3", Owner = "O3" }));
    list.SetAdapter(new GoldListAdapter(items));
    
  5. When you call "GetItem" and it returns a Java.Lang.Object, no problem, just refer to the .Item:

    GoldListJO item = (GoldListJO)GetItem(position);
    
    vh.txtName.Text = item.Item.Name;
    vh.txtUniqueId.Text = Java.Lang.String.Format("UniqueId: %s", item.Item.UniqueId);
    vh.txtOwner.Text = Java.Lang.String.Format("Owner: %s", item.Item.Owner);
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜