2012-02-06 60 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>