我在ExpendableListView Adapter類中有兩個數組,它們爲我的組和子數據頭提供了虛擬數據。第一個數組是一個常規數組,第二個數組是二維數組。如何動態地將數據添加到數組
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()
功能。
如何更換兩個數組(即使是2D的)一個數組列表? –
編輯我的帖子用一個例子 – Denny
在像'getGroup的實施方法()','getGroupView()','getChildView()'有喜歡的錯誤如下: '@覆蓋 公共對象getGroup(INT groupPosition) {group 0}返回groupNames [groupPosition]; }' 現在'groupNames'不承認'INT groupPosition' –