【问题标题】:Updating ExpandableListView with notifyDataSetChanged()使用 notifyDataSetChanged() 更新 ExpandableListView
【发布时间】: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


    【解决方案1】:

    您需要将某种DeleteMethod 添加到Adapter 并手动从Adapter 中删除该项目,而不仅仅是从List 中删除它。

    每次使用notifyDataSetChanged() 刷新视图时,Adapter 将调用List 周围的循环。由于您所做的更改,您在 Adapter 中的 List 会获得空值。

    【讨论】:

    • 听起来不错!你能给我举个例子吗,因为我不知道如何为适配器项编写删除方法..?
    • 好的,我想通了,它有效!这是我的 ListAdapter 中删除方法的代码:public void deleteItem (int nummer){stichpunkte.remove(nummer);ueberschriften.remove(nummer);}
    • 这就是我在主要活动中使用它的方式:Liste.deleteElement(groupPos);expListAdapter.deleteItem(groupPos);expListAdapter.notifyDataSetChanged();
    【解决方案2】:

    在你的 registerDataSetObserver 中调用 super 让 notifyDataSetChanged() 运行良好。

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        super.registerDataSetObserver(observer);    
    }
    

    【讨论】:

      【解决方案3】:

      你应该打电话给expListAdapter.notifyDataSetInvalidated()

      expListAdapter.notifyDataSetChanged() 只会更新您对每个项目的新值的视图(任何元素都不会更改)

      即它会再次为每个项目调用您的 getChildView 和 getGroupView。

      【讨论】:

        【解决方案4】:

        在我的情况下,我可以通过在调用 notifyDatasetChanged 之前清除关联 ExpandableListView 的焦点来解决此错误

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-16
          • 1970-01-01
          • 2021-06-07
          相关资源
          最近更新 更多