There is a class

public class AdapterHelper { final String ATR_GROUP_NAME = "gropuName"; final String ATR_PHONE_NAME = "phoneName"; String groups[]; String g_item[]; ArrayList<Map<String,String>> groupsData; ArrayList<Map<String,String>> childDataItem; ArrayList<ArrayList<Map<String,String>>> childData; Map<String,String> m; Context ctx; AdapterHelper(Context ctx, String groups[],String g_item[]) { this.ctx = ctx; this.groups = groups; this.g_item = g_item; } SimpleExpandableListAdapter adapter; SimpleExpandableListAdapter getAdapter() { groupsData = new ArrayList<Map<String, String>>(); for (String group : groups) { m = new HashMap<String, String>(); m.put(ATR_GROUP_NAME, group); groupsData.add(m); } String groupeFrom[] = {ATR_GROUP_NAME}; int groupTo[] = new int[]{android.R.id.text1}; childData = new ArrayList<ArrayList<Map<String, String>>>(); childDataItem = new ArrayList<Map<String, String>>(); for (String phone : g_item) { m = new HashMap<String, String>(); m.put(ATR_PHONE_NAME, phone); childDataItem.add(m); } childData.add(childDataItem); String childFrom[] = {ATR_PHONE_NAME}; int childTo[] = new int[]{android.R.id.text1}; adapter = new SimpleExpandableListAdapter(ctx, groupsData, simple_expandable_list_item_1, groupeFrom, groupTo, childData, android.R.layout.simple_list_item_1, childFrom, childTo); return adapter; } String getGroupText(int groupPos) { return ((Map<String,String>) (adapter.getGroup(groupPos))).get(ATR_GROUP_NAME); } String getChildText(int groupPos, int childPos) { return ((Map<String,String>) (adapter.getChild(groupPos, childPos))).get(ATR_PHONE_NAME); } String getGroupChildText(int groupPos, int childPos) { return getGroupText(groupPos) + " " + getChildText(groupPos, childPos); }} 

I want to call in the MainActivity, so that with each call I add a group and rows.

  private ExpandableListView expandableListView; private AdapterHelper ah; private SimpleExpandableListAdapter adapter; String groups[] = {"Group_1"}; String g_string[] = {"String","String_1","String_2"}; ah = new AdapterHelper(this,groups,g_string); adapter = ah.getAdapter(); expandableListView.setAdapter(adapter); 

Everything is added. But only for one group. What to remake with each individual call, added a new group with strings? As a result, I only add what was caused by the latter.

  String groups[] = {"Group_1"}; String strings_1[] = {"String_1","String_2","String_3"}; String groups1[] = {"Group_2"}; String strings_2[] = {"String_1","String_2","String_3"}; ah = new AdapterHelper(this,groups,strings_1); ah1 = new AdapterHelper(this,groups1,strings_2); adapter = ah.getAdapter(); adapter = ah1.getAdapter(); expandableListView.setAdapter(adapter); 

Ie not added, and overwrites the existing group.

I decided to try the following: Add a second parameter to the constructor, for example String g_item_2 [], and the second call to the method itself

 childDataItem = new ArrayList<Map<String, String>>(); for (String phone : g_item_2) { m = new HashMap<String, String>(); m.put(ATR_PHONE_NAME, phone); childDataItem.add(m); } childData.add(childDataItem); 

it turned out that if you call

  String groups[] = {"Group_11","TEST"}; String strings_1[] = {"String_11","String_2","String_3"}; String strings_2[] = {"String_1","String_2","String_3"}; textView = (TextView)findViewById(R.id.textView); ah = new AdapterHelper(this,groups,strings_1,strings_2); adapter = ah.getAdapter(); 

it adds as it should. But this is not the case, there is duplication of the code firstly, and secondly, the whole point is lost, how to use the loop to use the loop, in which the arrays of the strings in the constructor are searched And here is the problem, I can not understand how to build this cycle, or rather, as in the second iteration, substitute the following array of strings. For example:

 for(int a = 0; a < 2; a++) //Кол-во массивов строк в конструкторе { childDataItem = new ArrayList<Map<String, String>>(); for (int a =0; a < g_item.length; a++ ) { m = new HashMap<String, String>(); m.put(ATR_PHONE_NAME, g_item[a]); childDataItem.add(m); } childData.add(childDataItem); } 

ie, the first g_item array will be added. How in the following iteration to substitute the following array g_item_2?

upd: Did the following:

 String test[][]; 

in the constructor added

 AdapterHelper(Context ctx, String groups[], String[]... test) SimpleExpandableListAdapter getAdapter() { groupsData = new ArrayList<Map<String, String>>(); childData = new ArrayList<ArrayList<Map<String, String>>>(); // childDataItem = new ArrayList<Map<String, String>>(); for (int a = 0; a < groups.length; a++) //String group : groups { m = new HashMap<String, String>(); m.put(ATR_GROUP_NAME, groups[a]); groupsData.add(m); for (int i =0; i <test[a].length; i++ )//String phone : phonesHTC { //childData = new ArrayList<ArrayList<Map<String, String>>>(); childDataItem = new ArrayList<Map<String, String>>(); m = new HashMap<String, String>(); m.put(ATR_PHONE_NAME, test[a][i]); //phone childDataItem.add(m); } childData.add(childDataItem); // childDataItem.add(m); } 

... It turns out that groups are created, but only the last elements of the transmitted array are added to them. Error somewhere in the loop. I can not understand exactly where.

    1 answer 1

    You do not need a helper for list operations. So you have tangled yourself.
    You yes, first you create one adapter with some data, and then another one and assign it to the list.

    You just need to keep a link to the list / map with data in the adapter, add data to this list / Map and notify the adapter about changes in data.

    1. Create an adapter class
    2. Create a field in it to store this type of HashMap<String, ArrayList<String>>
    3. Create an add data method in the adapter ( put(String key, ArrayList<String> value) method put(String key, ArrayList<String> value) of the HashMap class)
    4. Immediately after adding, call the notifyDataSetChanged() adapter method.

    Those. you have one variable for the adapter, one adapter and one variable for storing data. You add to the latest data and notify the adapter about them.

    • Be an example if you have one. Thank. - Kamenev_D
    • @Kamenev_D, there is no example, but the algorithm is written. - Yuriy SPb
    • YuriySPb, after reading the Internet, found out that notifyDataSetChanged () does not work with ExpandableListView, but is designed to work with ListView. - Kamenev_D
    • @Kamenev_D, well, then try another developer.android.com/intl/ru/reference/android/widget/… method - Yuriy SPb