2014-07-07 1 views
0

사용자 정의 ExpandableListView가 있는데이 그룹이 비어 있으면 그룹 항목을 자동으로 삭제하려고합니다. 문제는 다음과 같습니다. 마지막 자식이 삭제 된 후 그룹을 삭제하면 getCount()가 올바른 숫자를 반환하지만 하위 그룹의 하위 항목은 두 배가됩니다!자동으로 그룹 항목을 삭제할 때 문제가 생깁니다.

삭제하기 전에 : 삭제 C1 후

g1={c1, c2}, g2={c3, c4} 

, C2 : 예를 들어

g2={c3, c4, c3, c4} 

전체 어댑터 코드 :

package base.util.ui.listview; 

import java.util.ArrayList; 
import java.util.Hashtable; 
import java.util.List; 

import android.util.Log; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseExpandableListAdapter; 

public abstract class ExpandableListAdapter extends BaseExpandableListAdapter { 
    private static final String TAG = ExpandableListAdapter.class 
      .getSimpleName(); 
    public List<IGroup> list; 
    private Hashtable<String, Integer> table; 

    public ExpandableListAdapter() { 
     this(null); 
    } 

    public ExpandableListAdapter(IGroup[] groups) { 
     list = new ArrayList<IGroup>(); 
     table = new Hashtable<String, Integer>(); 
     for (int i = 0; groups != null && i < groups.length; i++) { 
      addGroup(groups[i]); 
     } 
    } 

    public int addGroup(IGroup group) { 
     list.add(group); 
     table.put(group.getId(), list.indexOf(group)); 
     notifyDataSetChanged(); 
     return list.indexOf(group); 
    } 

    public void addChild(IGroup group, IChild child) { 
     Integer position = table.get(group.getId()); 
     if (position == null) { 
      position = addGroup(group); 
     } 

     if (position != -1) { 
      getGroup(position).addChild(child); 
      notifyDataSetChanged(); 
     } 
    } 

    public void removeGroup(int groupPosition) { 
     IGroup group = list.remove(groupPosition); 
     table.remove(group.getId()); 
     notifyDataSetChanged(); 
    } 

    public void removeChild(int groupPosition, int childPosition) { 
     try { 
      IGroup group = getGroup(groupPosition); 
      group.removeChild(childPosition); 
      notifyDataSetChanged(); 

      if (group.getChildCount() == 0) { 
       removeGroup(groupPosition);// ! 
      } 

      Log.i(TAG, "removeChild(): " + getChildrenCount()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

    public void clear() { 
     list.clear(); 
     table.clear(); 
     notifyDataSetChanged(); 
    } 

    public int getChildrenCount() { 
     int result = 0; 
     for (int i = 0; i < getGroupCount(); i++) { 
      result += getChildrenCount(i); 
     } 
     return result; 
    } 

    public boolean hasStableIds() { 
     return true; 
    } 

    public IGroup getGroup(int groupPosition) { 
     return list.get(groupPosition); 
    } 

    public IChild getChild(int groupPosition, int childPosition) { 
     return getGroup(groupPosition).getChild(childPosition); 
    } 

    public long getChildId(int groupPosition, int childPosition) { 
     return -1; 
     // return getChild(groupPosition, childPosition).childId; 
    } 

    public int getChildrenCount(int groupPosition) { 
     return getGroup(groupPosition).getChildCount(); 
    } 

    public int getGroupCount() { 
     return list.size(); 
    } 

    public long getGroupId(int groupPosition) { 
     return 0; 
    } 

    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 

    public abstract View getGroupView(final int groupPosition, 
      boolean isExpanded, View convertView, ViewGroup parent); 

    public abstract View getChildView(int groupPosition, int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent); 

    public void traverse(ITraverse task) { 
     for (int g = getGroupCount() - 1; g >= 0; g--) { 
      for (int c = getGroup(g).getChildCount() - 1; c >= 0; c--) { 
       task.onTraverse(g, c); 
      } 
     } 
    } 
} 

후 그 통지하십시오 나는 통신한다. removeChild()에서 removeGroup()을 호출하면이 문제가 다시 발생하지 않습니다. 아무도 정확하게 어떻게하는지 알려주세요. 고마워!

답변

1

이로 변경하고 그것을 작동 :

public long getGroupId(int groupPosition) { 
    return groupPosition; 
} 
관련 문제