使用notifyDataSetChanged()更新ExpandableListView

OnC*_*ner 9 android expandablelistview

首先简要概述一下我的代码,我希望它是可以理解的:

  • 我有一个ToDoElement带有一些变量的类.
  • 我有另一个类ToDoListe来管理ToDoElementArrayList中的s.
  • ToDoListe包含从ArrayList中deleteElement()删除a 的方法ToDoElement
  • 在我的主要活动中,liste_activity我创建了一个对象,ToDoListe并用一些对象填充它ToDoElement.
  • 在同一个活动中,我有一个带有我自己的适配器的ExpandableListView expListAdapter.
  • 适配器通过获取ToDoElements 的String变量来创建Groupviews和Childviews .
  • 我为列表中的每个Group项创建了一个ContextMenu,我使用该方法deleteElement().

好的,现在这里是我的问题:

我使用的方法后deleteElement()我想更新我的List,因为ArrayList的数据已ToDoListe更改.所以我打电话expListAdapter.notifyDataSetChanged().
但随后我的整个活动崩溃,原因是:" IndexOutOfBoundException:索引4无效,大小为4 "(ToDoELement我的列表中有5个项目,在删除其中一个之前).
我知道这是因为我的一个for循环,但我不知道为什么.

代码片段:

创建ToDoListe的新对象:

private static ToDoListe Liste = new ToDoListe();
Run Code Online (Sandbox Code Playgroud)

class 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();
     }
}
Run Code Online (Sandbox Code Playgroud)

定义列表适配器:

expListAdapter = new MyListAdapter(this, createGroupList(), createChildList());
setListAdapter( expListAdapter );
Run Code Online (Sandbox Code Playgroud)

为列表适配器创建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;
}
Run Code Online (Sandbox Code Playgroud)

我自己的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;
}
Run Code Online (Sandbox Code Playgroud)

使用notifyDataSetChanged():

Liste.deleteElement(groupPos);
expListAdapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)


非常感谢您的关注!!

dai*_*oor 5

您需要添加DeleteMethod一些类型Adapter并从Adapter手动中删除项目,而不仅从中删除它List.

每次使用notifyDataSetChanged()the 刷新视图时Adapter都会调用循环List.您ListAdapter因您所做的更改得到一个空值.


Len*_*Yan 5

在registerDataSetObserver meke notifyDataSetChanged()中调用super工作得很好.

@Override
public void registerDataSetObserver(DataSetObserver observer) {
    super.registerDataSetObserver(observer);    
}
Run Code Online (Sandbox Code Playgroud)