更新ExpandableListView与notifyDataSetChanged() [英] Updating ExpandableListView with notifyDataSetChanged()
问题描述
首先一个简短的概述我的code,我希望它可以理解的:
First a short overview of my code, i hope its understandable:
- 在我有一类叫做
ToDoElement
有一些变数。 - 在我有另一个类名为
ToDoListe
,其管理的ToDoElement
S在一个ArrayList -
ToDoListe
包含方法deleteElement()
删除ToDoElement
从ArrayList中 - 在我的主要活动
liste_activity
我创建ToDoListe
的对象,并使用一些对象填充它ToDoElement
。 - 在同一个活动,我有我自己的适配器名为
expListAdapter
的ExpandableListView。 - 在该适配器通过获取
ToDoElement
S的字符串变量创建Groupviews和Childviews。 - 在我创建了一个文本菜单的列表中的每个组项目,其中我使用的方法
deleteElement()
。
- I have a class called
ToDoElement
with a few variables. - I have another class called
ToDoListe
which manages theToDoElement
s in an ArrayList. ToDoListe
contains the methoddeleteElement()
to delete aToDoElement
from the ArrayList- In my main activity
liste_activity
i create an object ofToDoListe
and fill it with some Objects ofToDoElement
. - In the same activity i have an ExpandableListView with my own Adapter called
expListAdapter
. - The Adapter creates the Groupviews and Childviews by getting the String variables of the
ToDoElement
s. - I created a ContextMenu for every Group item of the list, in which i use the method
deleteElement()
.
好了,现在这里是我的问题:
之后,我使用的方法 deleteElement()
我想更新我的列表,因为ArrayList中的 ToDoListe
中的数据变化。所以我叫 expListAdapter.notifyDataSetChanged()
但后来我的整个活动崩溃,与理由:的 IndexOutOfBoundException:无效的指数4,大小为4 (我在我名单5 ToDoELement
项,删除其中的一个前)。照片我知道这是因为我的for循环中的一个,但我没有任何想法,为什么。
Ok, now here is my problem:
After i used the method deleteElement()
i want to update my List, because the data of the ArrayList in ToDoListe
changed. So i call expListAdapter.notifyDataSetChanged()
.
But then my whole activity crashes, with the reason: "IndexOutOfBoundException: Invalid index 4, size is 4" (I had 5 ToDoELement
items in my list, before deleting one of them).
I know that's because of one of my for-loops, but i don't have any idea why.
code片段:
ToDoListe的创建新的对象:
creating new Object of ToDoListe:
private static ToDoListe Liste = new ToDoListe();
类ToDoListe(只是重要的方法):
class ToDoListe (just the important methods):
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();
}
}
定义列表适配器:
define list Adapter:
expListAdapter = new MyListAdapter(this, createGroupList(), createChildList());
setListAdapter( expListAdapter );
创建的ArrayList的名单适配器:
create ArrayLists for list Adapter:
// 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;
}
我自己的清单适配器(只是重要的方法):
my own List Adapter (just the important methods):
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的():
use of notifyDataSetChanged():
Liste.deleteElement(groupPos);
expListAdapter.notifyDataSetChanged();
非常感谢您的关注!
Thanks a lot for your attention!!
推荐答案
您需要添加一个 DeleteMethod
某种到适配器<的/ code>并从
适配器项目
手动,它不但从列表
删除。
You need to add a DeleteMethod
of some sort to the Adapter
and remove the item from the Adapter
manually, not only remove it from the List
.
每次刷新的使用意见 notifyDataSetChanged()
的适配器
将随时待命,环列表
。你的列表
在适配器
得到一个空值,由于你所做的更改。
Each time you refresh the the views using notifyDataSetChanged()
the Adapter
will call the loop around the List
. Your List
in the Adapter
gets a null value due to the changes you made.
这篇关于更新ExpandableListView与notifyDataSetChanged()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!