gpt4 book ai didi

android - 对项目进行排序后更新可扩展的 ListView

转载 作者:行者123 更新时间:2023-11-29 18:13:50 24 4
gpt4 key购买 nike

概述:

  • 我有一个可扩展 ListView ,它显示 ToDoElement 类型的项目。
  • 每个 ToDoElement 项都有一个用于组元素的字符串和 1-3 个用于子元素的字符串
  • 我的自定义列表适配器 expListAdapter 使用方法 createGroupList()createChildList() 获取有关组项和子项的信息.
  • 在方法 createChildList() 中,我检查 ToDoElement 中有多少子项的字符串,并为适配器创建子项。
  • 最后,我可以通过比较组项的字符串来对列表进行排序。

所有这些工作正常,但这是我的问题:

  • List View中的项目排序正确后,子项目的数量是错误的。
  • 那是因为我的列表适配器不知道子项的数量已经改变。因此,一些子项没有显示,而其他子项只是空的,因为没有它们的数据。

我的建议:

我知道必须从我的适配器中再次调用方法 createGroupList()createChildList(),但我不知道该怎么做。
我试过expListAdapter.notifyDataSetInvalidated(),因为有人告诉我这个方法会调用createGroupList()createChildList() 再次,但不起作用。

代码 fragment :

我的列表适配器的定义:

expListAdapter = new MyListAdapter(this, createGroupList(), createGroupList());
setListAdapter( expListAdapter );

createGroupList() 和 createChildList():

    private List createGroupList() {    
ArrayList result = new ArrayList();
for (int i=0; i < Liste.AnzahlElemente(); i++) { //
result.add(Liste.getSize(i).getGroupString());
}
return result;
}

private List createChildList() {
ArrayList result = new ArrayList();
for(int i=0; i < Liste.getSize(); i++) {
ArrayList secList = new ArrayList();
for( int n = 1 ; n <= 3 ; n++ ) {
if (Liste.getElement(i).getChildString(n).length() != 0){
secList.add( "- " + Liste.getElement(i).getChildString(n));
}
}
result.add( secList );
}
return result;
}

排序列表:
(Liste是一个ToDoListe对象,它管理着Array List中的ToDoElement)

Collections.sort(Liste.getListe(), new NameComparator());

expListAdapter.notifyDataSetInvalidated();

//expListAdapter.notifyDataSetChanged(); //same effect as notifyDataSetInvalided

我的名字比较器:

public class NameComparator implements Comparator<ToDoElement>{

public int compare(ToDoElement item1, ToDoElement item2) {
return item1.getGroupString().compareTo(item2.getGroupString());
}
}


非常感谢您的帮助!!

最佳答案

我解决了我自己的问题!

我为我的自定义列表适配器编写了一个更新方法。

看起来像这样:

public class MyListAdapter extends BaseExpandableListAdapter{

private ArrayList<String> ueberschriften;
private ArrayList<ArrayList<String>> stichpunkte;

public MyListAdapter(Context context, List _ueberschriften, List _stichpunkte) {

this.ueberschriften = (ArrayList)_ueberschriften;
this.stichpunkte = (ArrayList)_stichpunkte;

inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

// [...] some other methods

public void update(List _GroupStrings, List _ChildStrings) {
GroupStrings = (ArrayList<String>) _GroupStrings;
ChildStrings = (ArrayList<ArrayList<String>>) _ChildStrings;
}
}

在我的 Activity 中对列表进行排序是这样的:

Collections.sort(Liste.getListe(), new NameComparator());

expListAdapter.update(createGroupList(), createChildList());

expListAdapter.notifyDataSetChanged();


现在子项的数量也正确更新了!
干杯!

关于android - 对项目进行排序后更新可扩展的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9277017/

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