我正在构建一个GPA跟踪器应用程序,我发现ExpandableListView是最合适的选择。功能如下:
用户单击FAB按钮并添加一个学期(组/标题)。 用户单击生成的Semester(组/标题)并添加课程。
我可以添加组,但我无法弄清楚如何为这些组动态添加子组件。基本上,我想获取被点击组的ID,启动alertDialog以让用户输入课程信息并将该信息添加到List。
我尝试使用setOnGroupClickListener(new ExpandableListView.OnGroupClickListener())
方法执行此操作,但现在我无法扩展该组。无论如何,这不是我的最终实施。我真的只是想让这个逻辑起作用。
这些是包含我的学期和课程信息的列表和HashMaps:
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;
这是我添加学期(小组/标题)的地方
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();
}
在我的onCreate()
中,我正在调用我的setOnGroupClickListener
方法来添加孩子:
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();
}
我想访问点击的群组ID,然后将我的孩子添加到该群组。
答案 0 :(得分:0)
在Dialog的界面OnClickListener中调用 notifyDataSetChanged(); :
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(); } });