OnC*_*ner 9 android expandablelistview
首先简要概述一下我的代码,我希望它是可以理解的:
ToDoElement
带有一些变量的类.ToDoListe
来管理ToDoElement
ArrayList中的s.ToDoListe
包含从ArrayList中deleteElement()
删除a 的方法ToDoElement
liste_activity
我创建了一个对象,ToDoListe
并用一些对象填充它ToDoElement
.expListAdapter
.ToDoElement
s 的String变量来创建Groupviews和Childviews .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)
非常感谢您的关注!!
您需要添加DeleteMethod
一些类型Adapter
并从Adapter
手动中删除项目,而不仅从中删除它List
.
每次使用notifyDataSetChanged()
the 刷新视图时Adapter
都会调用循环List
.您List
在Adapter
因您所做的更改得到一个空值.
在registerDataSetObserver meke notifyDataSetChanged()中调用super工作得很好.
@Override
public void registerDataSetObserver(DataSetObserver observer) {
super.registerDataSetObserver(observer);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21383 次 |
最近记录: |