namespace MyClass
{
public class MenuItem
{
public string Menu { get; set; }
public int MenuImage { get; set; }
}
}
namespace MyClass
{
[Activity (Label = "MainActivity", Icon = "@drawable/icon")]
public class MainActivity : Activity
{
private ListView _listView;
public static MenuItem[] menuItems = new MenuItem[0];
public static int[] menu_images = { Resource.Drawable.Icon, Resource.Drawable.Icon2};
public static string[] menu_value = { "Live Position", "Live Position2"};
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
_listView = FindViewById<ListView>(Resource.Id.listview);
menuItems = new MenuItem[menu_value.Length];
for (int i = 0; i < menu_value.Length; i++) {
menuItems [i] = new MenuItem ();
menuItems [i].Menu = menu_value [i];
menuItems [i].MenuImage = menu_images [i];
_menuItemsList.Add (menuItems [i]);
}
_listView.Adapter = new MyAdapter(this, _menuItemsList);
_listView.ItemClick += (object sender, Android.Widget.AdapterView.ItemClickEventArgs e) => {
Java.Lang.Object obj = e.Parent.GetItemAtPosition (e.Position);
MenuItem mt = (MenuItem) obj; // I'm unable to cast in this line it show's error as Cannot convert type 'Java.Lang.Object' to 'MyClass.MenuItem' (CS0030) (MyClass)
Console.WriteLine("Menu Clicked : " + mt.Menu);
};
}
}
}
I'm unable to cast in this line
"MenuItem mt = (MenuItem) obj;"
it show's error as Cannot convert type 'Java.Lang.Object' to 'MyClass.MenuItem' (CS0030) (MyClass)
Try this method for casting:
public static T Cast(Object obj) where T : class
{
var propertyInfo = obj.GetType().GetProperty("Instance");
return propertyInfo == null ? null : propertyInfo.GetValue(obj, null) as T;
}
Does this still work (I have the latest stable builds)? I tried the same thing with a Java.Lang.Object value from an intent extras field, but when executing the following line:
var propertyInfo = obj.GetType().GetProperty("Instance");
It would always return null on the Java.Lang.Object.
thanks,
Dennis
how to call caSt<> method if i have this code
var selectedTableItem = (TableItem)e.Parent.GetItemAtPosition(e.Position);
Answers
Try this method for casting:
public static T Cast(Object obj) where T : class
{
var propertyInfo = obj.GetType().GetProperty("Instance");
return propertyInfo == null ? null : propertyInfo.GetValue(obj, null) as T;
}
Thank u Eric Schmeck
Does this still work (I have the latest stable builds)? I tried the same thing with a Java.Lang.Object value from an intent extras field, but when executing the following line:
var propertyInfo = obj.GetType().GetProperty("Instance");
It would always return null on the Java.Lang.Object.
thanks,
Dennis
Yes it was working fine & I'm using it.
how to call caSt<> method if i have this code
Hi,
var propertyInfo = obj.GetType().GetProperty("Instance"); works fine.
Thanks for all. It helped me a lot.
Eric ... you saved my sorry ass. Beers are on me, my friend ...