mchd
mchd

Reputation: 3163

How to dynamically populate ExpandableListView through user input?

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:

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() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                semester = et_semester.getText().toString();
                listDataHeader.add(semester);
            }
        });

        builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            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:

expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            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() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        course = et_course.getText().toString();
                        listDataChild.put(listDataHeader.get(groupPosition), listCourses);
                    }
                });

                builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    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.

Upvotes: 2

Views: 239

Answers (1)

ely87
ely87

Reputation: 126

call notifyDataSetChanged(); inside your Dialog's interface OnClickListener:

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

Upvotes: 1

Related Questions