如何动态地向数组中添加数据
How to dynamically add data to arrays
我的 ExpendableListView 适配器 class 中有 2 个数组,它们为我的组和 child headers 提供虚拟数据。第一个数组是常规数组,第二个数组是二维数组。
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()
功能。
first of all to add data dynamically use ArrayList
- 无需实现onclick();在适配器中添加来自 activity onclick
的数据
- 然后使用notifyDatasetchanged();在适配器上,这将起到作用
数组的大小无法修改。如果你想要一个更大的数组,你必须实例化一个新的。更好的解决方案是使用可以根据需要增长的 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...);
或
List<String[]> childNames = new ArrayList<String[]>();
childNames.add(new String[]{"JAVA101", "MATH101", "CHEM101"});
//etc...
您还必须更改您的适配器以使用 ArrayLists 而不是 Arrays,我希望您明白了
我的 ExpendableListView 适配器 class 中有 2 个数组,它们为我的组和 child headers 提供虚拟数据。第一个数组是常规数组,第二个数组是二维数组。
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()
功能。
first of all to add data dynamically use ArrayList
- 无需实现onclick();在适配器中添加来自 activity onclick 的数据
- 然后使用notifyDatasetchanged();在适配器上,这将起到作用
数组的大小无法修改。如果你想要一个更大的数组,你必须实例化一个新的。更好的解决方案是使用可以根据需要增长的 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...);
或
List<String[]> childNames = new ArrayList<String[]>();
childNames.add(new String[]{"JAVA101", "MATH101", "CHEM101"});
//etc...
您还必须更改您的适配器以使用 ArrayLists 而不是 Arrays,我希望您明白了