简体   繁体   中英

MonoDroid SimpleExpandableListAdapter

This is as close as I've gotten to successfully populating an expandable listview from a sqlite table.

public class Today : ExpandableListActivity
{
   protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

       IList<IDictionary<string, object>> parent = new IList<IDictionary<string,object>>();
       IList<IList<IDictionary<string, object>>> child = new IList<IList<IDictionary<string,object>>>();

        External inst = new External();
        var connection = inst.conn();
        var c = connection.CreateCommand();
        c.CommandText = "Select Distinct Store From Calls";
        SqliteDataReader dr = c.ExecuteReader();
        if (dr.HasRows)
            while (dr.Read())
            {
                IDictionary<string, object> pItem = new IDictionary<string,object>();
                pItem.Add("Store", dr[0].ToString());
                parent.Add(pItem);
            }
       dr.Close();
       int cnt = parent.Count();

       if (cnt > 0)
       {
           IList<IDictionary<string, object>> children = new IList<IDictionary<string, object>>();
           foreach(IDictionary<string, object> d in parent)
           {
               c.CommandText = "Select CallNumber From Calls Where Store = '" + d.Values + "'";
               dr = c.ExecuteReader();
               while (dr.Read())
               {
                   IDictionary<string, object> childItem = new IDictionary<string, object>();
                   childItem.Add("Call", dr[0].ToString());
                   children.Add(childItem);
               }
               dr.Close();
           }
           child.Add(children);
       }

       SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, parent, Android.Resource.Layout.SimpleExpandableListItem1, new string[] { "Store" }, new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 }, child, Android.Resource.Layout.SimpleExpandableListItem2, new string[] { "CallNumber" }, new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 });
       SetListAdapter(adapter);
    }

}

This throws the following exceptions:

  • Cannot create an instance of the abstract class or interface 'System.Collections.Generic.IDictionary <string,object> ' X2
  • Cannot create an instance of the abstract class or interface 'System.Collections.Generic.IList <System.Collections.Generic.IDictionary X2

These exceptions really don't tell me why it can't create those instances or give me any clue as to what I'm doing incorrectly.

Those are build errors, not exceptions. The reason you're getting them is that you can't create an instance of an interface in C#. Instead you need to create an instance of an object that implements the interface you need. For example, using the ones in your code:

IDictionary<string, object> pItem = new Dictionary<string,object>();

IList<IDictionary<string, object>> children = new List<IDictionary<string, object>>();

This is valid because Dictionary<TKey, TValue> implements IDictionary<TKey, TValue> , and List<T> implements IList<T> .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM