[mono-android] problem with a Javalist (ArrayAdapter)
Jonathan Pryor
jonp at xamarin.com
Thu Jan 5 10:52:20 EST 2012
On Jan 5, 2012, at 9:36 AM, John Murray wrote:
> The array is a list of list thus List<List<string>>()
Is it really a List<List<string>>? Conceptually, sure, but actually?
> So I come to the click handler and want to extract the ‘Keyno’ from the array of the line selected
>
> I’ve got this code
> private void loglist_ItemClick(object sender, ItemEventArgs e)
> {
> int pos = Convert.ToInt32(e.Id);
> var selitem = loglist.Adapter.GetItem(pos);
What is the type of `loglist`? Of `loglist.Adapter`? Are you providing your own Adapter (by e.g. subclassing BaseAdapter), or are you using e.g. ArrayAdapter<T>?
`selitem` is very probably a Java.Lang.Object, as that's what BaseAdapter.GetItem(int) returns.
> var selitem2 = selitem.ToArray<string>();
> string sel2 = selitem2.ElementAt(0).ToString();
>
> Sorry about the various selitems I’ve been trying everything – the debugger tells me that the selitem is a JavaList
The debugger knows about the runtime types involved. The compiler does not. It sounds like you might be misunderstanding `var`. Your statement:
var selitem = loglist.Adapter.GetItem(pos);
is treated identically by the compiler as:
Java.Lang.Object selitem = loglist.Adapter.GetItem(pos);
`var` is not `dynamic`. :-)
If `selitem` is a JavaList, and you want to grab elements from it, cast it:
JavaList selitems = (JavaList) loglist.Adapter.GetItem(pos);
string sel2 = selitems[0].ToString();
Thanks,
- Jon
More information about the Monodroid
mailing list