• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to dynamically populate ExpandableListView through user input

Onur Ozbek

Lurker
Jul 4, 2017
2
0
I'm building a GPA tracker app and I find that an ExpandableListView is the most appropriate thing to use. The function is this:

The user clicks on a FAB button and adds a Semester (group/header). The user clicks on the generated Semester (group/header) and adds a course.

I can add the groups but I couldn't figure out how to dynamically add children for those groups. Basically, I want to get the ID of the clicked group, start an alertDialog to have the user input the course information and add that information to a List.

I tried to do that with the setOnGroupClickListener(new ExpandableListView.OnGroupClickListener()) method but now I can't expand the group. This wasn't my final implementation anyways. I really just wanted to get this logic to work.

These are the Lists and HashMaps that contain my Semester and Course information:

Code:
public class MainActivity extends Activity {

   ExpandableListAdapter listAdapter;
   ExpandableListView expListView;
   List<String> listDataHeader = new ArrayList<>();
   HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
   List<String>listCourses = new ArrayList<>();
   private String semester, course;

This is where I am adding my Semesters (group/header)

private void addSemester() {
       View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.parent_dialog_layout, null);
       final EditText et_semester = (EditText)dialogView.findViewById(R.id.et_semester);

       AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
       builder.setTitle("Create A Semester");
       builder.setView(dialogView);
       builder.setCancelable(false);

       builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
           [USER=1021285]@override[/USER]
           public void onClick(DialogInterface dialog, int which) {
               semester = et_semester.getText().toString();
               listDataHeader.add(semester);
           }
       });

       builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
           [USER=1021285]@override[/USER]
           public void onClick(DialogInterface dialog, int which) {
               dialog.cancel();
           }
       });
       listAdapter.notifyDataSetChanged();
       builder.show();
   }

And in my onCreate(), I'm calling my setOnGroupClickListener method to add the children:

Code:
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
           [USER=1021285]@override[/USER]
           public boolean onGroupClick(ExpandableListView parent, View v, final int groupPosition, long id) {
               parent.smoothScrollToPosition(groupPosition);

               View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.child_dialog_layout, null);
               final EditText et_course = (EditText)dialogView.findViewById(R.id.et_course);

               AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
               builder.setTitle("Create A Course");
               builder.setView(dialogView);
               builder.setCancelable(false);

               builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
                   [USER=1021285]@override[/USER]
                   public void onClick(DialogInterface dialog, int which) {
                       course = et_course.getText().toString();
                       listDataChild.put(listDataHeader.get(groupPosition), listCourses);
                   }
               });

               builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                   [USER=1021285]@override[/USER]
                   public void onClick(DialogInterface dialog, int which) {
                       dialog.cancel();
                   }
               });
               listAdapter.notifyDataSetChanged();
               builder.show();

               return true;
           }

           });
       listAdapter.notifyDataSetChanged();
       }

I want to access the clicked group's ID and then add my children to that group. I've been stuck on this problem for days now.
 
Last edited:

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones