MonoDroid SimpleExpandableListAdapter

时间:2012-02-06 22:38:11

标签: c# android xamarin.android

这与我从sqlite表成功填充可扩展列表视图一样接近。

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);
    }

}

这引发了以下例外情况:

  • 无法创建抽象类或接口的实例'System.Collections.Generic.IDictionary <string,object>'X2
  • 无法创建抽象类或接口的实例'System.Collections.Generic.IList <System.Collections.Generic.IDictionary X2

这些例外真的没有告诉我为什么它不能创建那些实例或给我任何关于我做错了什么的线索。

1 个答案:

答案 0 :(得分:2)

这些是构建错误,而不是例外。您获得它们的原因是您无法在C#中创建接口实例。相反,您需要创建一个实现所需接口的对象实例。例如,使用代码中的代码:

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

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

这是有效的,因为Dictionary<TKey, TValue>实施IDictionary<TKey, TValue>List<T>实施IList<T>