2012-05-24 35 views
1

我正在使用ExpandableListView,並且每個子行都有一個textview,並在其中包含複選框。Android:在ExpandableListView中動態標記複選框

我想要做的是,當用戶按下一個按鈕時,該活動會計算出哪些項目已「檢查」並對這些項目執行某些操作。

我讀了很多關於ExpandableListView和複選框目前不能正常工作的文章,以及我如何需要保留一個額外的數據結構來跟蹤已選擇的內容,而不是。這篇文章涵蓋了這個問題: ExpandableListView and checkboxes

完成!我現在可以跟蹤選擇的內容,以及未選擇的內容。

現在我想解決複選框不顯示其狀態的問題。我注意到,複選框隨意改變時:

  • 屏幕旋轉而改變
  • 一組摺疊/打開

我知道,它可以檢測這些事件。

我想知道是否有辦法手動設置複選框。我最初嘗試過

ExpandableListView elv = getExpandableListView(); 
for(int i = 0; i < elv.getChildCount(); i++) { 
    CheckBox checkbox = ... somehow get checbox within the list 
    checkbox.setChecked(checkList.at(i, j)); 
} 

其中,i和j表示哪個組,以及孩子和checkList跟蹤已檢查/未檢查的項目。

但是,我注意到getChildCount()的值取決於打開了多少個組,並且不一定告訴我我可以操作哪個列表項。

ExpandableListView已被使用,並且有人必須有辦法解決此問題。有沒有一個聰明的解決方案呢?具體而言,我正在尋找方法來檢查/取消選中onGroupExpand和onConfigurationChange(或類似事件)列表中的適當項目。

先謝謝您!

+0

做listitem(行)並讓它改變它的狀態? – Barak

+0

每次視圖可能更改時,我想檢查/取消選中複選框,或者直觀地更改列表。 –

回答

7

檢查下面的示例擴展列表適配器要點擊複選框本身,並更改它的狀態或點擊

/** 
    * A simple adapter which maintains an ArrayList of photo resource Ids. 
    * Each photo is displayed as an image. This adapter supports clearing the 
    * list of photos and adding a new photo. 
    * 
    */ 
    public class MyExpandableListAdapter extends BaseExpandableListAdapter { 


     // Sample data set. children[i] contains the children (String[]) for groups[i]. 
     private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" }; 
     private String[][] children = { 
       { "Arnold", "Barry", "Chuck", "David" }, 
       { "Ace", "Bandit", "Cha-Cha", "Deuce" }, 
       { "Fluffy", "Snuggles" }, 
       { "Goldy", "Bubbles" } 
     }; 

     private ArrayList<ArrayList<Boolean>> checkboxStatus = new ArrayList<ArrayList<Boolean>>(); 

     public MyExpandableListAdapter() { 
      int groupCount = groups.length; 
      for(int i=0; i<groupCount; i++) 
      { 
       int childCount = children[i].length; 
       ArrayList<Boolean> childStatus = new ArrayList<Boolean>(); 
       for(int j=0; j<childCount; j++) { 
        childStatus.add(false); 
       } 
       checkboxStatus.add(childStatus); 
      } 
     } 

     public Object getChild(int groupPosition, int childPosition) { 
      return children[groupPosition][childPosition]; 
     } 

     public long getChildId(int groupPosition, int childPosition) { 
      return childPosition; 
     } 

     public int getChildrenCount(int groupPosition) { 
      return children[groupPosition].length; 
     } 

     public TextView getGenericView() { 

      // Layout parameters for the ExpandableListView 
      AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, 64); 

      TextView textView = new TextView(ExpandableListCheckboxActivity.this); 
      textView.setLayoutParams(lp); 
      // Center the text vertically 
      textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
      // Set the text starting position 
      textView.setPadding(36, 0, 0, 0); 
      return textView; 
     } 

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

      CheckBox chkbox = new CheckBox(ExpandableListCheckboxActivity.this); 
      chkbox.setTag("" + groupPosition + "," + childPosition); 
      chkbox.setOnCheckedChangeListener(checkboxListener); 
      chkbox.setText(children[groupPosition][childPosition]); 

      chkbox.setChecked(checkboxStatus.get(groupPosition).get(childPosition)); 

      return chkbox; 
     } 

     public Object getGroup(int groupPosition) { 
      return groups[groupPosition]; 
     } 

     public int getGroupCount() { 
      return groups.length; 
     } 

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

     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, 
       ViewGroup parent) { 
      TextView textView = getGenericView(); 
      textView.setText(getGroup(groupPosition).toString()); 

      return textView; 
     } 

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

     public boolean hasStableIds() { 
      return true; 
     } 

     private OnCheckedChangeListener checkboxListener = new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       String positions = buttonView.getTag().toString(); 
       int groupPosition = Integer.valueOf(positions.split(",")[0]); 
       int childPosition = Integer.valueOf(positions.split(",")[1]); 
       checkboxStatus.get(groupPosition).set(childPosition, isChecked); 
      } 
     }; 

    } 
+0

謝謝!我會試試這個。 –

+0

@Kevin Adesara thanx你的代碼幫我... – Harsh

+0

我可以知道「ExpandableListCheckboxActivity」是什麼嗎? –

0

如果你只是想讓它在點擊該行更改:

elv.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
    @Override 
    public boolean onChildClick(ExpandableListView parent, View v, 
      int groupPosition, int childPosition, long id) { 
     CheckBox checkbox = (CheckBox) v.findViewById(r.id.yourcheckbox) 
     checkbox.setChecked(checkList.at(groupPosition, childPosition));  
     return true; 
    } 
}); 
+0

謝謝你的回答。我正在尋找我可以在onConfigurationChange(或onRotate ...類似的東西)和onGroupExpand(從可展開的列表視圖)上運行的東西,以供列表中的所有孩子使用。然而,當我選擇項目時,我已經在使用類似的東西來檢查/取消選中複選框。 –

+0

您是使用遊標還是數組來支持列表? – Barak

+0

我正在使用數組。 –