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