我的ExpendableListView Adapter类中有2个数组,它为我的组和子标头提供虚拟数据。第一个数组是常规数组,第二个数组是2D数组。
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
String[] groupNames = {
"Fall 2016", "Winter 2017",
"Fall 2017", "Winter 2018",
"Fall 2018", "Winter 2019",
"Fall 2019", "Winter 2020"
};
String[][] childNames = {
{"JAVA101", "MATH101", "CHEM101"},{"JAVA201", "MATH201", "CHEM201"},
{"JAVA301", "MATH301", "CHEM301"},{"JAVA401", "MATH401", "CHEM401"},
{"JAVA501", "MATH501", "CHEM501"},{"JAVA601", "MATH601", "CHEM601"},
{"JAVA701", "MATH701", "CHEM701"},{"JAVA801", "MATH801", "CHEM801"}
};
根据需要,我已经实现了所有必要的方法并将它们附加到我的数组中。
Context context;
public ExpandableListViewAdapter(Context context){
this.context = context;
}
@Override
public int getGroupCount() {
return groupNames.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return childNames[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groupNames[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childNames[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View view, ViewGroup parent) {
TextView textView = new TextView(context);
textView.setText(groupNames[groupPosition]);
textView.setPadding(100,0,0,0);
textView.setTextColor(Color.BLACK);
textView.setTextSize(20);
return textView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent) {
final TextView textView = new TextView(context);
textView.setText("Java" + groupNames[childPosition] + "01");
textView.setPadding(100,0,0,0);
textView.setTextColor(Color.BLACK);
textView.setTextSize(10);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, textView.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
return textView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
一切正常。我想要做的是通过FAB点击动态地将数据添加到我的ExpandableListView。我对我是否需要在我的适配器中实现我的FAB按钮的onClick()
功能感到非常好奇。
答案 0 :(得分:0)
首先动态添加数据使用ArrayList
答案 1 :(得分:0)
无法修改数组的大小。如果你想要一个更大的数组,你必须实例化一个新数组。更好的解决方案是使用可以根据需要增长的ArrayList
。
因此,在适配器中创建一个方法,将新项目添加到现有列表中。在该方法中,将新项添加到ArrayList并调用.notifyDataSetChanged()
。
将groupNames
替换为
List<String> places = Arrays.asList("Fall 2016", "Winter 2017", "Fall 2017", "Winter 2018", "Fall 2018", "Winter 2019", "Fall 2019", "Winter 2020");
对于childNames
,你必须这样做
List<String[]> childNames = Arrays.asList(new String[]{"JAVA101", "MATH101", "CHEM101"}, new String[]{ "JAVA201", "MATH201", "CHEM201" } etc...);
OR
List<String[]> childNames = new ArrayList<String[]>();
childNames.add(new String[]{"JAVA101", "MATH101", "CHEM101"});
//etc...
你还必须更改你的适配器以使用ArrayLists而不是Arrays,我希望你明白这个想法