2013-05-05 7 views
0

嗨,大家好我試圖通過單擊可展開列表中的子項來更改父項的值。我尋找一個解決方案,但無法找到任何東西。通過單擊可展開列表視圖中的子項來更改父項的值

public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int    childPosition, long id){ 
      ExpandableListAdapter itemAdapter = parent.getExpandableListAdapter(); 
      String selectedItem = (String)itemAdapter.getChild(groupPosition, childPosition); 
      groupHeader(selectedItem); 
      if(parent.isGroupExpanded(groupPosition)){ 
       parent.collapseGroup(groupPosition); 
      } 

      return true; 
     } 

    }); 

回答

0

ExpandableListView依賴於一個適配器來獲取其值。這些值來自某種數據結構,通常是一個ArrayList,甚至是一個簡單的數組。在onChildClick()中,傳遞對用於構建ExpandableListAdapter的數據結構的引用,並直接修改數據結構,然後調用notifyDataSetChanged()。

public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) 
{ 
     ExpandableListAdapter itemAdapter = parent.getExpandableListAdapter(); 
     String selectedItem = (String)itemAdapter.getChild(groupPosition, childPosition); 
     groupHeader(selectedItem); 
     if(parent.isGroupExpanded(groupPosition)) 
     { 
      parent.collapseGroup(groupPosition); 
     } 
     // your logic here 
     // expandableListArrayList.add() or expandableListArrayList.remove() 
     // or whatever 
     itemAdapter.notifyDataSetChanged(); 

     return true; 
    } 
}); 

您可能必須擴展適配器才能創建所需的功能。你的問題有點模糊,因爲我看不到你的代碼的整體結構,但這應該給你一個運行的開始。

相關問題