[英]MonoDroid SimpleExpandableListAdapter
這與我從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);
}
}
這會引發以下異常:
<string,object>
'X2 <System.Collections.Generic.IDictionary
X2 這些例外真的不告訴我為什么它不能創建那些實例或給我任何關於我正在做錯的線索。
這些是構建錯誤,而不是例外。 你得到它們的原因是你不能在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>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.