[mono-android] What´s wrong with this piece of code?
Bilik Tamás
biliktamas79 at gmail.com
Fri Mar 11 08:51:41 EST 2011
Hi!
I think, the problem is that you use the interfaced List and Dictionary
when calling the SimpleExpandableListAdapter constructor!
(groupData asIList<IDictionary<string, object>>
childData asIList<IList<IDictionary<string,object>>>)
Bilik Tamás
---------------------------------
On 2011.03.11. 14:39, Carlo Bolz wrote:
>
> Hm,
>
> I am not really sure what to do.
>
> I just want to port this example
> http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList3.html
> to monodroid.
>
> The problem is that the SimpleExpandableListAdapter want the
> interfaces for the „groupData“ and „childData“ lists.
>
> [Activity(Label = "ExpandableActivity", MainLauncher=true)]
>
> publicclassExpandableActivityTest: ExpandableListActivity
>
> {
>
> privatestaticstringNAME = "NAME";
>
> privatestaticstringISEVEN = "ISEVEN";
>
> protectedoverridevoidOnCreate(Bundlebundle)
>
> {
>
> base.OnCreate(bundle);
>
> SetContentView(Resource.Layout.Main);
>
> List<Dictionary<string, object>> groupData =
> newList<Dictionary<string, object>>();
>
> List<List<Dictionary<string, object>>> childData =
> newList<List<Dictionary<string, object>>>();
>
> for(inti = 0; i < 20; i++)
>
> {
>
> Dictionary<string, object> curGroupMap = newDictionary<string, object>();
>
> curGroupMap.Add(NAME, "Group "+ i.ToString());
>
> curGroupMap.Add(ISEVEN, (i % 2 == 0) ? "This group is
> even": "This group is odd");
>
> groupData.Add(curGroupMap);
>
> List<Dictionary<string, object>> children = newList<Dictionary<string,
> object>>();
>
> for(intj = 0; j < 15; j++)
>
> {
>
> Dictionary<string, object> curChildMap = newDictionary<string, object>();
>
> curChildMap.Add(NAME, "Child "+ j.ToString());
>
> curChildMap.Add(ISEVEN, (j % 2 == 0) ? "This child
> is even": "This child is odd");
>
> children.Add(curChildMap);
>
> }
>
> childData.Add(children);
>
> }
>
> // Now the app dies here
>
> SimpleExpandableListAdapterexpListAdapter =
>
> newSimpleExpandableListAdapter(
>
> this, groupData asIList<IDictionary<string, object>>,
>
> Resource.Layout.GroupRow,
>
> newString[] { NAME },
>
> newint[] { Resource.Id.childname },
>
> childData asIList<IList<IDictionary<string,object>>>,
>
> Resource.Layout.ChildRow,
>
> newString[] { NAME, ISEVEN },
>
> newint[] { Resource.Id.childname, Resource.Id.rgb });
>
> SetListAdapter(expListAdapter);
>
> }
>
> }
>
> *Von:*monodroid-bounces at lists.ximian.com
> [mailto:monodroid-bounces at lists.ximian.com] *Im Auftrag von *Jay R. Wren
> *Gesendet:* Freitag, 11. März 2011 14:16
> *An:* monodroid at lists.ximian.com
> *Betreff:* [***SPAM*** Score/Req: 06.2/5.0] Re: [mono-android] What´s
> wrong with this piece of code?
>
> Morten is right.
>
> IList<T> is not covariant in T.
>
> Interfaces that were updated when 4.0 shipped can be found here :
> http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx
>
> However, I think this should have generated a compiler error. I'm 90%
> sure that CSC.EXE would not compile that code.
> --
> Jay
>
> On 3/11/2011 7:53 AM, Morten Nilsen wrote:
>
> Hi
>
> I suspect this might be caused by your use of IList/IDictionary for
> variables..
>
> I’m not sure a List<Dictionary<string,object>> would accept an object
> of type IDictionary<string,object>..
>
> What if, one day, the curGroupMap is changed to be an instance of
> MusicalDictionary<string,object>?
>
> You either need to do groupData.Add((Dictionary<string,
> object>)curGroupMap) or stop casting everything into interfaces, I
> believe.
>
> If you really do want to have interfaces rather than classes here,
> your list needs to agree with that.
>
> Not everything has to be an interface, by the way – especially not
> internally in a method.
>
> -- mvh, Morten
>
> *From:*monodroid-bounces at lists.ximian.com
> <mailto:monodroid-bounces at lists.ximian.com>
> [mailto:monodroid-bounces at lists.ximian.com] *On Behalf Of *Carlo Bolz
> *Sent:* Friday, March 11, 2011 1:36 PM
> *To:* Monodroid at lists.ximian.com <mailto:Monodroid at lists.ximian.com>
> *Subject:* [mono-android] What´s wrong with this piece of code?
>
> Hello,
>
> what´s wrong with this piece of code:
>
> IList<IDictionary<string, object>> groupData =
> newList<Dictionary<string, object>>() asIList<IDictionary<string,
> object>>;
>
> IList<IList<IDictionary<string, object>>> childData =
> newList<List<Dictionary<string, object>>>()
> asIList<IList<IDictionary<string, object>>>;
>
> for(inti = 0; i < 20; i++)
>
> {
>
> IDictionary<string, object> curGroupMap = newDictionary<string, object>();
>
> curGroupMap.Add(NAME, "Group "+ i.ToString());
>
> curGroupMap.Add(ISEVEN, (i % 2 == 0) ? "This group is
> even": "This group is odd");
>
> groupData.Add(curGroupMap); // < the app dies here
>
> Any suggestions?
>
>
>
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com <mailto:Monodroid at lists.ximian.com>
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
>
>
> _______________________________________________
> Monodroid mailing list
> Monodroid at lists.ximian.com
>
> UNSUBSCRIBE INFORMATION:
> http://lists.ximian.com/mailman/listinfo/monodroid
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110311/95b329e0/attachment-0001.html
More information about the Monodroid
mailing list