1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to dynamically populate ExpandableListView through user input

Discussion in 'Android Development' started by Onur Ozbek, Jul 4, 2017.

  1. Onur Ozbek

    Onur Ozbek Lurker
    Thread Starter

    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 (Text):
    1.  
    2. public class MainActivity extends Activity {
    3.  
    4.    ExpandableListAdapter listAdapter;
    5.    ExpandableListView expListView;
    6.    List<String> listDataHeader = new ArrayList<>();
    7.    HashMap<String, List<String>> listDataChild = new HashMap<String, List<String>>();
    8.    List<String>listCourses = new ArrayList<>();
    9.    private String semester, course;
    10.  
    11. This is where I am adding my Semesters (group/header)
    12.  
    13. private void addSemester() {
    14.        View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.parent_dialog_layout, null);
    15.        final EditText et_semester = (EditText)dialogView.findViewById(R.id.et_semester);
    16.  
    17.        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    18.        builder.setTitle("Create A Semester");
    19.        builder.setView(dialogView);
    20.        builder.setCancelable(false);
    21.  
    22.        builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
    23.            [USER=1021285]@override[/USER]
    24.            public void onClick(DialogInterface dialog, int which) {
    25.                semester = et_semester.getText().toString();
    26.                listDataHeader.add(semester);
    27.            }
    28.        });
    29.  
    30.        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
    31.            [USER=1021285]@override[/USER]
    32.            public void onClick(DialogInterface dialog, int which) {
    33.                dialog.cancel();
    34.            }
    35.        });
    36.        listAdapter.notifyDataSetChanged();
    37.        builder.show();
    38.    }
    39.  
    And in my onCreate(), I'm calling my setOnGroupClickListener method to add the children:

    Code (Text):
    1.  
    2. expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    3.            [USER=1021285]@override[/USER]
    4.            public boolean onGroupClick(ExpandableListView parent, View v, final int groupPosition, long id) {
    5.                parent.smoothScrollToPosition(groupPosition);
    6.  
    7.                View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.child_dialog_layout, null);
    8.                final EditText et_course = (EditText)dialogView.findViewById(R.id.et_course);
    9.  
    10.                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    11.                builder.setTitle("Create A Course");
    12.                builder.setView(dialogView);
    13.                builder.setCancelable(false);
    14.  
    15.                builder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
    16.                    [USER=1021285]@override[/USER]
    17.                    public void onClick(DialogInterface dialog, int which) {
    18.                        course = et_course.getText().toString();
    19.                        listDataChild.put(listDataHeader.get(groupPosition), listCourses);
    20.                    }
    21.                });
    22.  
    23.                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
    24.                    [USER=1021285]@override[/USER]
    25.                    public void onClick(DialogInterface dialog, int which) {
    26.                        dialog.cancel();
    27.                    }
    28.                });
    29.                listAdapter.notifyDataSetChanged();
    30.                builder.show();
    31.  
    32.                return true;
    33.            }
    34.  
    35.            });
    36.        listAdapter.notifyDataSetChanged();
    37.        }
    38.  
    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.
     


    #1 Onur Ozbek, Jul 4, 2017
    Last edited: Jul 4, 2017

    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...