【发布时间】:2012-02-13 12:20:07
【问题描述】:
首先是我的代码的简短概述,我希望它可以理解:
- 我有一个名为
ToDoElement的类,其中包含一些变量。 - 我有另一个名为
ToDoListe的类,它管理 ArrayList 中的ToDoElements。 -
ToDoListe包含方法deleteElement()从 ArrayList 中删除ToDoElement - 在我的主要活动
liste_activity中,我创建了一个ToDoListe的对象并用ToDoElement的一些对象填充它。 - 在同一个活动中,我有一个 ExpandableListView 和我自己的适配器,名为
expListAdapter。 - 适配器通过获取
ToDoElements 的字符串变量来创建Groupviews 和Childviews。 - 我为列表中的每个 Group 项目创建了一个 ContextMenu,我在其中使用了
deleteElement()方法。
好的,现在这是我的问题:
在我使用deleteElement() 方法后,我想更新我的列表,因为ToDoListe 中的 ArrayList 的数据发生了变化。所以我打电话给expListAdapter.notifyDataSetChanged()。
但是我的整个活动崩溃了,原因是:“IndexOutOfBoundException: Invalid index 4, size is 4”(我的列表中有 5 个ToDoELement 项目,在删除其中一个之前)。
我知道这是因为我的一个 for 循环,但我不知道为什么。
代码片段:
创建新的 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;
}
我自己的 List Adapter(只是重要的方法):
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();
非常感谢您的关注!!
【问题讨论】:
标签: android expandablelistview