gpt4 book ai didi

android - 如何将子项动态添加到可扩展 ListView 中。?

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:32 25 4
gpt4 key购买 nike

例如,我有一个编辑文本和一个按钮。当按下按钮时,文本被添加到我的可扩展 ListView 中的第一组,并且下面的每个项目都被添加到它下面。谢谢你。 (如果您需要我到目前为止的代码,请索取。)

最佳答案

第一个你应该创建你自己的自定义适配器。一旦创建了一个这样的类。

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

然后当你需要添加 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();

关于android - 如何将子项动态添加到可扩展 ListView 中。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15348412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com