2

나는 ExpandableListView을 통해 NotfiyDataSetChanged을 업데이트하는 데 어려움을 겪고 있습니다.Android : SimpleExpandableListAdapter에서 NotifyDataSetChanged를 올바르게 사용하는 방법?

웹 서버에서 데이터를 다운로드하고 있는데, 이는 ExpandableListView에 올바르게 표시됩니다.

하위 항목을 클릭하면 사용자가 값을 변경할 수있는 대화 상자가 표시됩니다. 이 값은 ExpandableListView에 나타나기 때문에 클래스의 백그라운드 데이터를 변경하고 NotifyDataSetChanged()으로 전화하십시오. 이것은 웹 서버에 새로운 요청을 보낼 때까지 하위 항목에 대해 잘 작동합니다. 그렇다면 내 다운로드 기능이나 로컬 변경 기능에서도 NotifyDataSetChanged()이 작동하지 않습니다.

마찬가지로 대화 상자 다음에 그룹 항목 콘텐츠를 변경하려는 경우 내 그룹의 배경 데이터도 변경합니다. 그러나 NotifyDataSetChanged()은 하위 항목에 대해 최소한 몇 번 작동하지만 그룹 항목에는 전혀 영향을 미치지 않습니다.

내 배경 데이터가 실제로 변경되면 디버거 & log.d()를 확인했지만 그럴 수 있습니다.

this questionthis thread을 살펴보면, 나는 notifyData ... 잘못된 것을 사용하고 있다고 생각합니다. 하지만 어떻게 작동해야하는지 알 수 없습니다. 내가 찾은 유용한 기능은 registerDataSetObserver(observer)뿐 이었지만이 방법도 잘 사용하지 못했습니다. :/

여기 내 코드가 있습니다. 필자가 필요한 부분까지 뜯어 내려고 시도 했으므로 약간의 오류가있을 수 있습니다.

@SuppressWarnings("rawtypes") 
private List children; 
@SuppressWarnings("rawtypes") 
private List groups; 
private SimpleExpandableListAdapter expListAdapter; 


/*download from webserver*/ 
void function1() 
{ 
    groups = new ArrayList(); 
    children = new ArrayList(); 

    */go through a list I downloaded*/ 
    for (...) 
    { 
     HashMap groupMap = new HashMap(); 
     groupMap.put(groupName, downloadedList.getName()); 
     groupMap.put(groupRate, downloadedList.getMatchrate() + getString(R.string.matchrate)); 
     groups.add(groupMap); 

     ArrayList childList = new ArrayList(); 
     HashMap childMap; 

     */go through a sublist and fill child Map accordingly*/ 
     for (...) 
     { 
      childMap = new HashMap(); 
      childMap.put(...); 
      childList.add(childMap); 
     } 

     children.add(childList); 

    } 

    if (expListAdapter == null) 
    { 
     expListAdapter = new SimpleExpandableListAdapter(KnBDiagMan.this, groups, 
       R.layout.xlist_group_item, 
       new String[] 
       { groupName, groupRate }, //private final static String components of my activity 
       new int[] 
       { R.id.title, R.id.rate }, 
       children, 
       R.layout.xlist_child_item, 
       new String[] 
       { childName, childValue }, 
       new int[] 
       { R.id.child_title, R.id.child_value } 
     ) 
     { 
      @Override 
      public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
      { 
       /*change of the text color of R.id.child_value's depending on content*/ 
      } 
     }; 

     setListAdapter(expListAdapter); 
    } 
    else 
    { 
     expListAdapter.notifyDataSetChanged(); 
    } 
} 

/*change after Dialog*/ 
void function2() 
{ 
    String savedChildName = (String) ((HashMap) ((ArrayList) children.get(groupPos)).get(childPos)).get(childName); 

    for (int i = 0; i < children.size(); ++i) 
    { 
     List secondLevel = (ArrayList) children.get(i); 

     for (int j = 0; j < (secondLevel.size()); ++j) 
     { 
      HashMap map = (HashMap) secondLevel.get(j); 
      String compareName = (String) map.get(childName); 

      if (compareName.contentEquals(savedChildName)) 
      { 
       HashMap newMap = new HashMap(); 
       newMap.put(childName, savedChildName); 
       newMap.put(childValue, newValue); 
       secondLevel.set(j, newMap); 
      } 
     } 
    } 

    List newGroups = new ArrayList(); 

    for(int i = 0; i < groups.size(); ++i) 
    { 
     String savedGroupName = (String) ((HashMap) groups.get(i)).get(groupName); 
     HashMap groupContent = new HashMap(); 
     groupContent.put(groupName, savedGroupName); 
     groupContent.put(groupRate, getString(R.string.newString)); 
     newGroups.add(groupContent); 
    } 

    groups = newGroups; 

    expListAdapter.notifyDataSetChanged(); 
} 

업데이트 : notifyDataSetChanged를 호출하는 대신 새로운 SimpleExpandableListAdapter을 만들 경우 모든들이 의도로 노력하고 있습니다. 그러나 이것이 이것이 해결책이 될 수 없다고 확신합니다. 어댑터를 사용하는 데있어서 중요한 점은 무엇입니까?

답변

6

당신은 대신에 ... 새로운 참조하여 SimpleExpandableListAdapter의 데이터 객체에 대한 해당 지역의 참조를 대체하고 있습니다 :

List newGroups = new ArrayList(); 
... 
groups = newGroups; 

보십시오 : 마법처럼

groups.clear(); 

// Add items here 
... 

expListAdapter.notifyDataSetChanged(); 
+0

작품, 그래서 지금까지 감사합니다 많은! :) – jellyfish