2013-03-11 38 views
2

例如,我有一個編輯文本和一個按鈕。當按下按鈕時,文本將添加到我的可擴展列表視圖中的第一個組,並且每個以下項目都將添加到其下方。謝謝。 (如果您需要的代碼我到目前爲止請請求。)如何將子項動態添加到可展開的列表視圖中。

+0

我stuckeed here.Can您分享您的代碼嗎? – Niroj 2016-09-26 04:18:30

回答

5

第一個你應該創建你的自定義適配器。 曾經創建過這樣的類。

public class ExpandableListParentClass{ 

    private Object parent; 
    private ArrayList<Object> parentChildren; 


    public ExpandableListParentClass() { 
    } 
    public ExpandableListParentClass(Object parent, ArrayList<Object> parentChildren) { 
     this.parent = parent; 
     this.parentChildren = parentChildren; 
    } 

    public Object getParent() { 
     return parent; 
    } 

    public void setParent(Object parent) { 
     this.parent = parent; 
    } 

    public ArrayList<Object> getParentChildren() { 
     return parentChildren; 
    } 

    public void setParentChildren(ArrayList<Object> parentChildren) { 
     this.parentChildren = parentChildren; 
    } 
} 

,並創建您的適配器

public class ExpandableListAdapter extends BaseExpandableListAdapter implements Filterable{ 

    private LayoutInflater inflater; 
    private ArrayList<ExpandableListParentClass<Object>> mParent; 
    private View view; 


    public ArrayList<Object> getMParent() { 
     return mParent; 
    } 



    public ExpandableListAdapter(Context context, 
     ArrayList<ExpandableListParentClass<Object>> parentList) { 
     this.mParent = parentList; 
     this.inflater = LayoutInflater.from(context); 

    } 

    // counts the number of group/parent items so the list knows how many 
    // times calls getGroupView() method 
    public int getGroupCount() { 
     return mParent.size(); 
    } 

    // counts the number of children items so the list knows how many times 
    // calls getChildView() method 
    public int getChildrenCount(int parentPosition) { 
     int size =0; 
     if(mParent.get(parentPosition).getParentChildren() != null){ 
     size = mParent.get(parentPosition).getParentChildren().size(); 
     } 
     return size; 
    } 

    // gets the title of each parent/group 
    public Object getGroup(int i) { 
     return mParent.get(i).getParent(); 
    } 

    // gets the name of each item 
    public Object getChild(int parentPosition, int childPosition) { 
     return mParent.get(parentPosition).getParentChildren().get(childPosition); 
    } 

    public long getGroupId(int parentPosition) { 
     return parentPosition; 
    } 

    public long getChildId(int i, int childPosition) { 
     return childPosition; 
    } 

    public boolean hasStableIds() { 
     return true; 
    } 

    // in this method you must set the text to see the parent/group on the list 

    public View getGroupView(int parentPosition, boolean b, View view, ViewGroup viewGroup) { 
     if (view == null) { 
      view = inflater.inflate(R.layout.layout_listview, viewGroup, false); 
     } 

     return view; 

    } 

    // in this method you must set the text to see the children on the list 

    public View getChildView(int parentPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) { 
     if (view == null) { 
     view = inflater.inflate(R.layout.layout_listview, viewGroup, false); 
     } 



     // return the entire view 
     return view; 
    } 

    public boolean isChildSelectable(int i, int i1) { 
     return true; 
    } 

    } 



now fill to list 


     List<ExpandableListParentClass> arrayParents = new ArrayList<ExpandableListParentClass>(); 
     ExpandableListParentClass item = new ExpandableListParentClass(); 
     item.setParent(yourParentItem); 
     item.setParentChildren(yourChildList); 

當你需要添加子

ExpandableListView myExpList= (ExpandableListView) this.view.findViewById(R.id.yourExpListView); 

ExpandableListAdapter adapter = 
     (ExpandableListAdapter) myExpList.getExpandableListAdapter(); 

((ExpandableListParentClass)adapter.getMParent().get(0)).getParentChildren().add(object); 
//(change to get(0) which you parent want to get) 
adapter.notifyDataSetChanged(); 
adapter.notifyDataSetInvalidated(); 
+1

將新的字符串添加到列表時,此代碼是否會立即更新列表?它應該是 – 2013-03-11 23:45:25

+1

。我想 – KEYSAN 2013-03-12 08:09:56

+0

你嘗試過嗎? – KEYSAN 2013-03-14 19:57:10

相關問題