如何动态地添加子项到一个可扩展的列表视图? [英] How do I add child items dynamically to an expandable listview.?

查看:254
本文介绍了如何动态地添加子项到一个可扩展的列表视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个编辑文本和一个按钮。当按钮pressed文本被添加到第一组中的我的膨胀列表视图和每个下列项目添加它的下面。谢谢。 (如果您需要code我迄今请申请。)

For example, I have an edit text and a button. When the button is pressed the text is added to the first group in my expandable listview and each following item is added underneath it. Thank you. (If you need the code I have so far please request.)

推荐答案

第一个你要创建自定义的适配器。 一旦创建一个类是这样的。

first one you should be create your your custom adapter. once create a class like this.

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);

之后,当你需要添加子

after that when you need to add child

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

这篇关于如何动态地添加子项到一个可扩展的列表视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆