首先是我的代码的简短概述,我希望它是可以理解的:
- 我有一个名为
ToDoElement
的类,其中包含一些变量。 - 我有另一个名为
ToDoListe
的类,它管理 ArrayList 中的ToDoElement
。 ToDoListe
包含用于从 ArrayList 中删除ToDoElement
的方法deleteElement()
- 在我的主要 Activity
liste_activity
中,我创建了一个ToDoListe
对象,并用ToDoElement
的一些对象填充它。 - 在同一个 Activity 中,我有一个 ExpandableListView 和我自己的名为
expListAdapter
的适配器。 - 适配器通过获取
ToDoElement
的字符串变量创建组 View 和 subview 。 - 我为列表中的每个组项目创建了一个 ContextMenu,我在其中使用方法
deleteElement()
。
好的,现在这是我的问题:
在我使用deleteElement()
方法后,我想更新我的列表,因为ToDoListe
中ArrayList 的数据发生了变化。所以我调用了 expListAdapter.notifyDataSetChanged()
。
但随后我的整个 Activity 崩溃了,原因是:“IndexOutOfBoundException:索引 4 无效,大小为 4”(我的列表中有 5 个 ToDoELement
项,然后删除其中一个)。
我知道这是因为我的一个 for 循环,但我不知道为什么。< br/>
代码 fragment :
创建 ToDoListe 的新对象:
private static ToDoListe Liste = new ToDoListe();
类 ToDoListe(只是重要的方法):
public class ToDoListe {
private ArrayList<ToDoElement> ToDoListe;
public ToDoListe()
{
ToDoListe = new ArrayList<ToDoElement>();
}
public void newElement(ToDoElement element){
ToDoListe.add(element);
}
public void deleteElement(int nummer) {
ToDoListe.remove(nummer);
}
public int AnzahlElemente() {
return ToDoListe.size();
}
}
定义列表适配器:
expListAdapter = new MyListAdapter(this, createGroupList(), createChildList());
setListAdapter( expListAdapter );
为列表适配器创建 ArrayLists:
// creates the list with the group items
private List createGroupList() {
ArrayList result = new ArrayList();
for (int i=0; i < Liste.AnzahlElemente(); i++) {
result.add(Liste.getElement(i).getUeberschrift());
}
return result;
}
// creates the list with the child items
private List createChildList() {
ArrayList result = new ArrayList();
for(int i=0; i < Liste.AnzahlElemente(); i++) {
ArrayList secList = new ArrayList();
for( int n = 1 ; n <= 3 ; n++ ) {
if (Liste.getElement(i).getStichpunkt(n).length() != 0){
secList.add( "- " + Liste.getElement(i).getStichpunkt(n));
}
}
result.add( secList );
}
return result;
}
我自己的列表适配器(只是重要的方法):
public class MyListAdapter extends BaseExpandableListAdapter{
private ArrayList<String> ueberschriften;
private ArrayList<ArrayList<String>> stichpunkte;
private LayoutInflater inflater;
public MyListAdapter(Context context, List _ueberschriften, List _stichpunkte) {
this.ueberschriften = (ArrayList)_ueberschriften;
this.stichpunkte = (ArrayList)_stichpunkte;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_child, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.item_child_title);
tv.setText(liste_activity.getListe().getElement(groupPosition).getStichpunkt(childPosition));
return convertView;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_group, null);
}
TextView tv = (TextView)convertView.findViewById(R.id.item_group_title);
tv.setText(liste_activity.getListe().getElement(groupPosition).getUeberschrift());
return convertView;
}
使用 notifyDataSetChanged():
Liste.deleteElement(groupPos);
expListAdapter.notifyDataSetChanged();
非常感谢您的关注!!
最佳答案
您需要向 Adapter
添加某种类型的 DeleteMethod
并手动从 Adapter
中删除该项目,而不仅仅是从中删除它列表
。
每次您使用 notifyDataSetChanged()
刷新 View 时,Adapter
都会围绕 List
调用循环。由于您所做的更改,您在 Adapter
中的 List
获得空值。
关于android - 使用 notifyDataSetChanged() 更新 ExpandableListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9260409/