android– 如何动态地将子项添加到可展开的列表视图中.

android– 如何动态地将子项添加到可展开的列表视图中.,第1张

概述例如,我有一个编辑文本和一个按钮.按下按钮时,文本将添加到我的可扩展列表视图中的第一个组中,并在其下方添加每个后续项目.谢谢.(如果您需要我到目前为止的代码,请索取.)解决方法:首先,您应该创建自己的自定义适配器.一旦创建了这样的类.publicclassExpandableListParentCla

例如,我有一个编辑文本和一个按钮.按下按钮时,文本将添加到我的可扩展列表视图中的第一个组中,并在其下方添加每个后续项目.谢谢. (如果您需要我到目前为止的代码,请索取.)

解决方法:

首先,您应该创建自己的自定义适配器.
一旦创建了这样的类.

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();
总结

以上是内存溢出为你收集整理的android – 如何动态地将子项添加到可展开的列表视图中.全部内容,希望文章能够帮你解决android – 如何动态地将子项添加到可展开的列表视图中.所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1110095.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存