如何动态地向可展开的列表视图中添加子项?

3
例如,我有一个EditText和一个按钮。当点击按钮时,文本将被添加到可展开列表视图中的第一组,并且随后的每个项目都将添加在其下方。谢谢。(如果需要我目前的代码,请请求。)

我在这里卡住了。你能分享一下你的代码吗? - Niroj
1个回答

7

首先,您应该创建自己的自定义适配器。创建一个类如下所示。

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
当向列表中添加新字符串时,这段代码会立即更新列表吗? - Wayne B Jackson
虽然我没有完全使用你的解决方案,但你指引了我正确的方向。 - Wayne B Jackson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接