2017-05-27 59 views
0

我在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()功能。

回答

0

數組的大小不能被修改。如果你想要一個更大的數組,你必須實例化一個新的數組。更好的解決方案是使用可在需要時增長的ArrayList

因此,在適配器中創建一個方法,將新項目添加到現有列表中。在該方法中,將新項目添加到ArrayList並調用.notifyDataSetChanged()

List<String> places = Arrays.asList("Fall 2016", "Winter 2017", "Fall 2017", "Winter 2018", "Fall 2018", "Winter 2019", "Fall 2019", "Winter 2020"); 

更換groupNames對於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... 

你也必須改變你的適配器使用的ArrayList,而不是陣列,我希望你能明白

+0

如何更換兩個數組(即使是2D的)一個數組列表? –

+0

編輯我的帖子用一個例子 – Denny

+0

在像'getGroup的實施方法()','getGroupView()','getChildView()'有喜歡的錯誤如下: '@覆蓋 公共對象getGroup(INT groupPosition) {group 0}返回groupNames [groupPosition]; }' 現在'groupNames'不承認'INT groupPosition' –

0

首先要添加數據動態地使用ArrayList

  1. 沒有必要的onclick執行();在適配器中添加來自活動的數據onclick
  2. 然後使用notifyDatasetchanged();在適配器上這將做詭計
相關問題