2012-02-06 82 views
0

这和我从sqlite表成功填充可展开列表视图一样。MonoDroid SimpleExpandableListAdapter

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

这些异常真的不说我为什么不能创建这些实例,或给我提供任何我不正确做法的线索。

回答

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>