-1

I'm new in android. I have expandable list view of course in which a topic has n lectures and add data like this:

List<String> Topic1 = new ArrayList<String>();
Topic1.add("Lecture 1");
Topic1.add("Lecture 2");
Topic1.add("Lecture 3");
Topic1.add("Lecture 4");
Topic1.add("Lecture 5");

List<String> Topic2 = new ArrayList<String>();
Topic2.add("Lecture 1");
Topic2.add("Lecture 2");
Topic2.add("Lecture 3");
Topic2.add("Lecture 4");
Topic2.add("Lecture 5");

List<String> Topic3 = new ArrayList<String>();
Topic3.add("Lecture 1");
Topic3.add("Lecture 2");
Topic3.add("Lecture 3");
Topic3.add("Lecture 4");
Topic3.add("Lecture 5");

expandableListDetail.put("Topic 1", Topic1);
expandableListDetail.put("Topic 2", Topic2);
expandableListDetail.put("Topic 3", Topic3);

But I want efficient way to create List and add values at run time if there is more Topics and its lectures but didn't get how to do that.

James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

0

You should use a RecyclerView instead, its like a ListView but way more functional. You just create an Adapter class to handle all your items and then whenever your list is updated, the changes are propagated throughout by calling adapter.notifyDatasetChanged();

Here's a good article on getting started, Recycler View Basics.

0

Try out this if your app is server side: First create List of list

List<List<String>> topicDataList = new ArrayList<List<String>>();

Create parent and child List and insert response data in it and then

for (int j = 0; j < parentList.size(); j++) {
            List<String> tempList = new ArrayList<String>();

            for (int i = 0; i < childList.size(); i++) {

                    tempList.add(childList.get(i).getChild_name());

                }
            }
            topicDataList.add(tempList);

        }
        for (int k = 0; k < parentList.size(); k++) {

            expandableListDetail.put(parentList.get(k).getChild_name(), topicDataList.get(k));
        }
return expandableListDetail;