Bug 5319 - From Java.Util.String to System.Object, then to String
Summary: From Java.Util.String to System.Object, then to String
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 4.2.x
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-05-25 05:16 UTC by Maxim
Modified: 2012-05-30 14:40 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Maxim 2012-05-25 05:16:20 UTC
Latest update improve some in filing ListView with a data, but loose some in selecting

        private JavaList<IDictionary<string, object>> LpuList;

Filling a data:
            LpuList = new JavaList<IDictionary<string, object>>();
            foreach (Lpu lpu in rawLpuList)
                JavaDictionary<string, object> map = new JavaDictionary<string, object>();
                map.Add("clpuname", lpu.Name);
                map.Add("cadress", lpu.Address.AddressForShow);  
                map.Add("NID", lpu.Id);
            listViewAdapter = new SimpleAdapter(this, LpuList, Android.Resource.Layout.SimpleListItem2, columns, listItemIds);

Handle item click:
        protected void ProceedItemClick(Context context, AdapterView.ItemClickEventArgs args)
            object nkey;
            LpuList[args.Position].TryGetValue("NID", out nkey);
			// trying to do something different
			//string nkey = (string)LpuList[args.Position]["NID"];
            foreach (Lpu item in rawLpuList) {
                if (item.Id == (string)nkey) {
				//if (item.Id == nkey) {
					Util.Log("LPU selected: " + item.Id);

Got InvalidCast at TryGetValue:
System.InvalidCastException: Value is not a convertible object: Java.Lang.String to System.Object
  at System.Convert.ToType (object,System.Type,System.IFormatProvider,bool) [0x00228] in /Users/builder/data/lanes/monodroid-mac-4.2-series/804357b4/source/mono/mcs/class/corlib/System/Convert.cs:2627
  at System.Convert.ChangeType (object,System.Type) [0x00040] in /Users/builder/data/lanes/monodroid-mac-4.2-series/804357b4/source/mono/mcs/class/corlib/System/Convert.cs:2183
  at Java.Interop.JavaConvert.FromJniHandle<object> (intptr,Android.Runtime.JniHandleOwnership) <0x00373>
  at Android.Runtime.JavaDictionary`2<string, object>.Get (string) <0x00223>
  at Android.Runtime.JavaDictionary`2<string, object>.TryGetValue (string,object&) <0x0005b>
  at EMIAS.SelectLPUActivity.ProceedItemClick (Android.Content.Context,Android.Widget.AdapterView/ItemClickEventArgs) [0x00000] in /Users/fvs/Projects/EMIAS/EMIAS_Android/EMIAS/SelectLPUActivity.cs:101
  at EMIAS.SelectLPUActivity.<CreateList>m__25 (object,Android.Widget.AdapterView/ItemClickEventArgs) [0x00000] in /Users/fvs/Projects/EMIAS/EMIAS_Android/EMIAS/SelectLPUActivity.cs:93
  at Android.Widget.AdapterView/IOnItemClickListenerImplementor.OnItemClick (Android.Widget.AdapterView,Android.Views.View,int,long) [0x0000b] in /Users/builder/data/lanes/monodroid-mac-4.2-series/804357b4/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.Widget.AdapterView.cs:231
  at Android.Widget.AdapterView/IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (intptr,intptr,intptr,intptr,int,long) [0x00018] in /Users/builder/data/lanes/monodroid-mac-4.2-series/804357b4/source/monodroid/src/Mono.Android/platforms/android-10/src/generated/Android.Widget.AdapterView.cs:168
  at (wrapper dynamic-method) object.37abe62a-6de3-482b-b922-4a7be2d8470d (intptr,intptr,intptr,intptr,int,long) <IL 0x00024, 0x0008b>
Comment 1 Jonathan Pryor 2012-05-29 17:17:27 UTC
Simpler test code:

	var map = new JavaDictionary<string, object> ();
	map.Add ("a", "string");
	map.Add ("b", 42);
	object v;
	map.TryGetValue ("a", out v);

Results in:

> Unhandled Exception: System.InvalidCastException: Value is not a convertible object: Java.Lang.String to System.Object
>   at System.Convert.ToType (System.Object value, System.Type conversionType, IFormatProvider provider, Boolean try_target_to_type) [0x00000] in <filename unknown>:0 
>   at System.Convert.ChangeType (System.Object value, System.Type conversionType) [0x00000] in <filename unknown>:0 
>   at Java.Interop.JavaConvert.FromJniHandle[Object] (IntPtr handle, JniHandleOwnership transfer) [0x00000] in <filename unknown>:0 
>   at Android.Runtime.JavaDictionary`2[System.String,System.Object].Get (System.String key) [0x00000] in <filename unknown>:0 
>   at Android.Runtime.JavaDictionary`2[System.String,System.Object].TryGetValue (System.String key, System.Object& value) [0x00000] in <filename unknown>:0 

Thank you for the bug report.
Comment 2 Jonathan Pryor 2012-05-30 14:40:55 UTC
Fixed in b2faf6e0. This will be in the 4.2.3 release.