[mono-android] What´s wrong with this piece of code?

Carlo Bolz cb at dm-edv.de
Fri Mar 11 08:39:08 EST 2011


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)]

    public class ExpandableActivityTest : ExpandableListActivity

    {

        private static string NAME = "NAME";

        private static string ISEVEN = "ISEVEN";

 

        protected override void OnCreate(Bundle bundle)

        {

            base.OnCreate(bundle);

 

            SetContentView(Resource.Layout.Main);

 

            List<Dictionary<string, object>> groupData = new List<Dictionary<string, object>>();

            List<List<Dictionary<string, object>>> childData = new List<List<Dictionary<string, object>>>();

 

            for (int i = 0; i < 20; i++)

            {

                Dictionary<string, object> curGroupMap = new Dictionary<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 = new List<Dictionary<string, object>>();

                for (int j = 0; j < 15; j++)

                {

                    Dictionary<string, object> curChildMap = new Dictionary<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

SimpleExpandableListAdapter expListAdapter =

                new SimpleExpandableListAdapter(

                    this, groupData as IList<IDictionary<string, object>>,

                    Resource.Layout.GroupRow,

                    new String[] { NAME },

                    new int[] { Resource.Id.childname },

                    childData as IList<IList<IDictionary<string,object>>>,

                    Resource.Layout.ChildRow,

                    new String[] { NAME, ISEVEN },

                    new int[] { 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] On Behalf Of Carlo Bolz
Sent: Friday, March 11, 2011 1:36 PM
To: 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 = new List<Dictionary<string, object>>() as IList<IDictionary<string, object>>;

            IList<IList<IDictionary<string, object>>> childData = new List<List<Dictionary<string, object>>>() as IList<IList<IDictionary<string, object>>>;

 

            for (int i = 0; i < 20; i++)

            {

                IDictionary<string, object> curGroupMap = new Dictionary<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
 
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/43f51638/attachment-0001.html 


More information about the Monodroid mailing list