Hello!

I have in the project (for example) 4 types of data: a chair, a table, a sofa, a couch, they are all heirs to furniture class. It is necessary in one window to display a list of furniture, divided into groups (chairs, tables, etc.). For this I use ExpandableListView, and expand BaseAdapter. Prior to this, the adapter received a String and there were no problems (well, of course). Now, I want in this one adapter to send lists of objects of the above types, referring to which, I will be able to fill in subgroups. Those. looks like this:

Π‘Ρ‚ΡƒΠ»ΡŒΡ стул 1 стул 2 Π‘Ρ‚ΠΎΠ»Ρ‹ стол 1 Π”ΠΈΠ²Π°Π½Ρ‹ Π΄ΠΈΠ²Π°Π½ 1 

.... When I used one data type I did this:

 ArrayList<ArrayList<String>> groups = new ArrayList<>(); List<String> mChair= new ArrayList<>(); List<String> mTable= new ArrayList<>(); .... groups.add(mChair); groups.add(mTable); 

And groups in the adapter, along with the context.

And when it was necessary to display on the screen:

  groups.get(groupPosition).get(childPosition)); 

And how now to send lists of objects to the adapter and combine them into groups.

Simply put, how can I send objects of different types to the adapter, and get the necessary data from these objects for display in the list with my list item?

    1 answer 1

    For example, like this:

     /** названия Π³Ρ€ΡƒΠΏΠΏ ΠΌΠ΅Π±Π΅Π»ΠΈ */ String[] stul; //1. Π‘Ρ‚ΡƒΠ» String[] diwan; //2. Π΄ΠΈΠ²Π°Π½ /** названия Π³Ρ€ΡƒΠΏΠΏ ΠΌΠ΅Π±Π΅Π»ΠΈ */ String[] mebelGroups; /** коллСкция для Π³Ρ€ΡƒΠΏΠΏ */ ArrayList<Map<String, String>> groupData; /** коллСкция для элСмСнтов ΠΎΠ΄Π½ΠΎΠΉ Π³Ρ€ΡƒΠΏΠΏΡ‹ */ ArrayList<Map<String, String>> childDataItem; /** общая коллСкция для ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΉ элСмСнтов */ ArrayList<ArrayList<Map<String, String>>> childData; /** список Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚ΠΎΠ² Π³Ρ€ΡƒΠΏΠΏΡ‹ ΠΈΠ»ΠΈ элСмСнта */ Map<String, String> m; ExpandableListView explvMebel; /** Π“Ρ€ΡƒΠΏΠΏΡ‹ ΠΎΠΏΠ΅Ρ€Π°Ρ‚ΠΎΡ€ΠΎΠ² */ mebelGroups = getResources().getStringArray(R.array.mebelGroups); // Массив ΠΈΠ· Ρ‚ΠΈΠΏΠΎΠ² ΠΌΠ΅Π±Π΅Π»ΠΈ /** ΠšΠΎΠΌΠΏΠΎΠ½Π΅Π½Ρ‚Ρ‹ Π³Ρ€ΡƒΠΏΠΏ */ stul= getResources().getStringArray(R.array.stulArray); //1. Массив ΡΡ‚ΡƒΠ»ΡŒΠ΅Π² diwan= getResources().getStringArray(R.array.diwanArray); //2. Массив Π΄ΠΈΠ²Π°Π½ΠΎΠ² /** заполняСм ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡŽ Π³Ρ€ΡƒΠΏΠΏ ΠΈΠ· массива с названиями Π³Ρ€ΡƒΠΏΠΏ */ groupData = new ArrayList<Map<String, String>>(); for (String group : mebelGroups) { /** заполняСм список Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚ΠΎΠ² для ΠΊΠ°ΠΆΠ΄ΠΎΠΉ Π³Ρ€ΡƒΠΏΠΏΡ‹ */ m = new HashMap<String, String>(); m.put("groupName", group); groupData.add(m); } /** список Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚ΠΎΠ² Π³Ρ€ΡƒΠΏΠΏ для чтСния */ String groupFrom[] = new String[] {"groupName"}; /** список ID view-элСмСнтов, Π² ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΠΌΠ΅Ρ‰Π΅Π½Ρ‹ Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚Ρ‹ Π³Ρ€ΡƒΠΏΠΏ */ int groupTo[] = new int[] {android.R.id.text1}; /** создаСм ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡŽ для ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΉ элСмСнтов */ childData = new ArrayList<ArrayList<Map<String, String>>>(); // Ρ‚ΠΈΠΏΡ‹ ΡΡ‚ΡƒΠ»ΡŒΠ΅Π² /** 1. Π‘Ρ‚ΡƒΠ»ΡŒΡ коллСкция */ childDataItem = new ArrayList<Map<String, String>>(); // заполняСм список Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚ΠΎΠ² для ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ элСмСнта for (String operChild : stul) { m = new HashMap<String, String>(); m.put("mebelName", operChild); childDataItem.add(m); } // добавляСм Π² ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡŽ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΠΉ childData.add(childDataItem); /** 2. Π΄ΠΈΠ²Π°Π½ΠΎΠ² коллСкция */ childDataItem = new ArrayList<Map<String, String>>(); for (String operChild : diwan) { m = new HashMap<String, String>(); m.put("mebelName", operChild); childDataItem.add(m); } childData.add(childDataItem); /** список Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚ΠΎΠ² элСмСнтов для чтСния */ String childFrom[] = new String[] {"mebelName"}; /** список ID view-элСмСнтов, Π² ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠΎΠΌΠ΅Ρ‰Π΅Π½Ρ‹ Π°Ρ‚Ρ€ΠΈΠ±ΡƒΡ‚Ρ‹ элСмСнтов */ int childTo[] = new int[] {android.R.id.text1}; SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groupData, android.R.layout.simple_expandable_list_item_1, groupFrom, groupTo, childData, android.R.layout.simple_list_item_1, childFrom, childTo); explvMebel = (ExpandableListView) findViewById(R.id.expandableListView1); explvMebel.setAdapter(adapter); 

    It seems to be so. This is a working code just to display. I still screwed the auto-close previously open, if necessary, I can share it.