android – 添加数据时刷新ExpandableListView:保持状态

android – 添加数据时刷新ExpandableListView:保持状态,第1张

概述我使用BaseExpandableListAdapter使用自定义适配器填充了ExpandableListView.我可以在新活动中添加新数据,然后我必须刷新列表.刷新列表时,我想保持扩展相同的组. 可以在列表中的任何位置添加新条目.我们可以添加新组或仅添加新组. 现在我正在使用: adapter.notifyDataSetChanged(); 列表刷新,但状态不一样.例如,如果我有三个组:G1扩 我使用Baseexpandablelistadapter使用自定义适配器填充了ExpandableListVIEw.我可以在新活动中添加新数据,然后我必须@R_279_6419@.@R_279_6419@时,我想保持扩展相同的组.

可以在列表中的任何位置添加新条目.我们可以添加新组或仅添加新组.

现在我正在使用:

adapter.notifyDataSetChanged();

列表刷新,但状态不一样.例如,如果我有三个组:G1扩展,G2未扩展,G3扩展,我在G2和G3之间添加一个新组G4,然后G4获得G3状态,因为它占据了它的位置.有没有办法自动保持状态或我必须在我的适配器中手动执行?

谢谢!

[编辑]

添加一些代码.

这是我的适配器:

public class StopsInnerexpandablelistadapter extends Baseexpandablelistadapter {private ArrayList<String> groups; // Dates.private ArrayList<ArrayList<HashMap<String,String>>> children; // HashMap has stop ID,image and price.private Context context;public StopsInnerexpandablelistadapter (Context ctx,ArrayList<String> groups,ArrayList<ArrayList<HashMap<String,String>>> children) {    context = ctx;    this.groups = groups;    this.children = children;}@OverrIDepublic boolean areAllitemsEnabled() {    return true;}public HashMap<String,String> getChild(int groupposition,int childposition) {    return children.get(groupposition).get(childposition);}public long getChildID(int groupposition,int childposition) {    return childposition;}public VIEw getChildVIEw(int groupposition,int childposition,boolean isLastChild,VIEw convertVIEw,VIEwGroup parent) {    if (convertVIEw == null) {        // Inflate child's vIEw.        LayoutInflater infalinflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        convertVIEw = infalinflater.inflate(R.layout.stop_row,null);    }    // Get the stop data and use it to fill the child's vIEws.    HashMap<String,String> child = children.get(groupposition).get(childposition);    ...    return convertVIEw;}public int getChildrenCount(int groupposition) {    return children.get(groupposition).size();}public String getGroup(int groupposition) {    return groups.get(groupposition);}public int getGroupCount() {    return groups.size();}public long getGroupID(int groupposition) {    return groupposition;}public VIEw getGroupVIEw(int groupposition,boolean isExpanded,VIEwGroup parent) {     if (convertVIEw == null) {        // Inflate group's vIEw.        LayoutInflater infalinflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        convertVIEw = infalinflater.inflate(R.layout.stop_subgroup,null);    }    String date = groups.get(groupposition);    TextVIEw dateVIEw = (TextVIEw) convertVIEw.findVIEwByID(R.ID.Title_date);    dateVIEw.setText(date);    return convertVIEw;}public boolean hasStableIDs() {    return true;}public boolean isChildSelectable(int arg0,int arg1) {    return true;}public voID updateData(ArrayList<String> groups,String>>> children) {    this.groups = groups;    this.children = children;}}

在我的活动中,我这样做:

protected voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.stop_List);    mExpandableList = (ExpandableListVIEw)findVIEwByID(R.ID.expandable_List);    model = new MyModel(this);}@OverrIDeprotected voID onResume() {    super.onResume();    ListEntrIEs entrIEs = model.getEntrIEs();    if(adapter == null){        // Sets the adapter that provIDes data to the List.        adapter = new StopsInnerexpandablelistadapter(this,entrIEs.getDates(),entrIEs.getChilds());        mExpandableList.setAdapter(adapter);    }    else{        adapter.updateData(entrIEs.getDates(),entrIEs.getChilds());        adapter.notifyDataSetChanged();    }}
解决方法 ExpandableListVIEw没有正确重新扩展,因为您没有使用稳定的ID.使用稳定的ID将为您解决问题.

虽然你通过这里启用了稳定的ID:

public boolean hasStableIDs() {    return true;}

您仍然需要使用getGroupID()和getChildID()方法实际返回稳定的ID.分别返回groupposition和childposition不符合稳定ID的条件.

要成为稳定的ID意味着返回的值在整个数据集中是唯一的.此外,无论给定项目的位置如何,返回的值都是相同的.例如,假设您正在存储人员.每个人都有一个独特的SSN.这将是一个很好的稳定ID,可以通过getChildID()返回.例如,如果约翰史密斯位于第2,4位(组,子),然后稍后移动到位置(3,1)……他的稳定ID(或SSN)仍将是相同的.仅返回位置编号不能保证这一点.

总结

以上是内存溢出为你收集整理的android – 添加数据时刷新ExpandableListView:保持状态全部内容,希望文章能够帮你解决android – 添加数据时刷新ExpandableListView:保持状态所遇到的程序开发问题。

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

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

原文地址:https://54852.com/web/1126867.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存