ExpandableListView父/子分组问题

时间:2012-02-07 15:47:25

标签: android xamarin.android

我从包含服务调用信息的sqlite表填充ExpandableListView。我希望父组是每个唯一的服务站点,子组是包含该站点上所有呼叫的呼叫信息。

Site A
  Call 1
Site B
  Call 2

相反,我得到了:

Site A
  Call 1
  Call 2
Site B
  Call 1
  Call 2

每个服务电话都显示在每个网站下。我用ExpandableListViews几天来一直撞墙而且我能够使用的唯一代码没有产生各种错误的冲击,或者应用程序崩溃了

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

        SetContentView(Resource.Layout.Boards);

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

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

       if (cnt > 0)
       {
           List<IDictionary<string, object>> children = new List<IDictionary<string, object>>();
           foreach(IDictionary<string, object> d in parent)
           {
               foreach (KeyValuePair<string, object> k in d)
               {
                   var store = k.Value.ToString().Substring(0, k.Value.ToString().IndexOf(" "));
                   c.CommandText = "Select CallNumber || ' ' || Description From Calls Where Store = '" + store + "'";
                   dr = c.ExecuteReader();
                   while (dr.Read())
                   {
                       Dictionary<string, object> childItem = new Dictionary<string, object>();
                       childItem.Add("Call", dr[0].ToString());
                       children.Add(childItem);
                   }
                   dr.Close();
                   child.Add(children);
               }

           }              
       }
       ExpandableListView view = (ExpandableListView)FindViewById(Android.Resource.Id.List);
       IExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, parent, Android.Resource.Layout.SimpleExpandableListItem1, new string[] { "Store", "Call" }, new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 }, child, Android.Resource.Layout.SimpleExpandableListItem2, new string[] { "Store", "Call" }, new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 });
       view.SetAdapter(adapter);
    }

}

1 个答案:

答案 0 :(得分:1)

您正在将childItem添加到相同的List(子项),然后在子项上多次添加此列表。

您必须为ExpandableListView

上的每个组创建一个List<List<>>