ExpandableListView父/子分组问题 [英] ExpandableListView Parent/Child Grouping Issue

查看:178
本文介绍了ExpandableListView父/子分组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从含服务呼叫信息的SQLite表填充一个 ExpandableListView 。我想父组是每一个独特的服务站点和子组包含在该站点的所有来电呼叫信息。

  A地盘
  致电1
站点B
  召唤2

相反,我越来越:

  A地盘
  致电1
  召唤2
站点B
  致电1
  召唤2

每个服务呼叫显示出来的每个站点下。我一直在敲打我的头靠在墙上用 ExpandableListViews 为天,只有code我已经能够使用尚未产生的冲击各种错误或应用程序崩溃仅仅是

 公共类今日:ExpandableListActivity
{
   保护覆盖无效的OnCreate(束束)
    {
        base.OnCreate(包);        的setContentView(Resource.Layout.Boards);        清单<&IDictionary的LT;字符串对象>>父=新的List<&IDictionary的LT;字符串对象>>();
        清单<&IList的LT; IDictionary的<字符串对象>>>孩子=新的List<&IList的LT; IDictionary的<字符串对象>>>();        外部出师表=新的外部();
        VAR连接= inst.conn();
        变种C = connection.CreateCommand();
        c.CommandText =SELECT DISTINCT商店||''||市||','||国家召唤而来;
        connection.Open();
        SqliteDataReader博士= c.ExecuteReader();
        如果(dr.HasRows)
            而(dr.Read())
            {
                字典<字符串对象> pItem =新词典<字符串对象>();
                pItem.Add(商店,博士[0]的ToString());
                parent.Add(pItem);
            }
       dr.Close();
       INT CNT = parent.Count();       如果(CNT大于0)
       {
           清单<&IDictionary的LT;字符串对象>>孩子=新的List<&IDictionary的LT;字符串对象>>();
           的foreach(IDictionary的<字符串对象> d在父)
           {
               的foreach(KeyValuePair<字符串对象> K的四)
               {
                   。变种商店= k.Value.ToString()子串(0,k.Value.ToString()的IndexOf());
                   c.CommandText =选择CallNumber ||''||说明从哪里呼叫商店='+实体店+';
                   博士= c.ExecuteReader();
                   而(dr.Read())
                   {
                       字典<字符串对象> childItem =新词典<字符串对象>();
                       childItem.Add(呼叫,博士[0]的ToString());
                       children.Add(childItem);
                   }
                   dr.Close();
                   child.Add(儿童);
               }           }
       }
       ExpandableListView观点=(ExpandableListView)FindViewById(Android.Resource.Id.List);
       IExpandableListAdapter适配器=新SimpleExpandableListAdapter(这一点,父母,Android.Resource.Layout.SimpleExpandableListItem1,新的String [] {店,呼叫},新的INT [] {Android.Resource.Id.Text1,Android.Resource.Id .Text2},孩子,Android.Resource.Layout.SimpleExpandableListItem2,新的String [] {店,呼叫},新的INT [] {Android.Resource.Id.Text1,Android.Resource.Id.Text2}) ;
       view.SetAdapter(适配器);
    }}


解决方案

您要添加的childItem相同的列表(儿童),然后加入这个名单不止一次的孩子。

您必须创建一个列表与LT;名单<>> 有关ExpandableListView每组

I am populating an ExpandableListView from a sqlite table containing service call information. I want the parent group to be each unique service site and the child group to contain the call information for all calls at that site.

Site A
  Call 1
Site B
  Call 2

Instead, I'm getting:

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

Every service call is showing up under every site. I've been banging my head against the wall with ExpandableListViews for days now and the only code I've been able to use that hasn't generated an onslaught of various errors or the app just crashing is

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

}

解决方案

You are adding the childItem to the same List (children) and then adding this list more than once on child.

You must create a List<List<>> for every group on the ExpandableListView

这篇关于ExpandableListView父/子分组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆