2011-08-27 244 views
1

我使用ExpandableListView。在小組和小孩中,都有複選框。如何做到以下要求?檢查ExpandableListView(安卓)所有複選框

當用戶檢查組的複選框,檢查其孩子的所有框。

我想我需要覆蓋BaseExpandableListAdapter, 但我不知道應該填寫什麼。謝謝。

public View getGroupView(int groupPosition, boolean isExpanded, 
    View convertView, ViewGroup parent) 
{ 
    CheckBox cb = (CheckBox)v.findViewById(R.id.checkBox1); 
    cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, 
      boolean isChecked) 
     { 
      //Fill in what? 
     } 

    }); 

} 

也有同樣的問題,但我不明白它的解決方案: How to get all childs view in group with ExpandableListView

回答

2

鏈接的答案,我認爲是說你應該保持跟蹤你的檢查組的狀態的一個數組列表。然後,在你的BaseExpandableListAdapter您getChildView方法,你會做檢查,以查看是否爲子組存在..

這裏是我的實現,我使用一個HashMap,而不是一個ArrayList:

public View getChildView(int groupPosition, int childPosition, 
     boolean isLastChild, View convertView, ViewGroup parent) { 
    CheckBox check__bookmark = (CheckBox)child_row.findViewById(R.id._checkBox); 

    if (mapp.group_bookmark_s_checked.containsKey((groupPosition))==true)    
     check__bookmark.setChecked(true); 
    else 
     check__bookmark.setChecked(false); 

}

public View getGroupView(int groupPosition, boolean isExpanded, 
     View convertView, ViewGroup parent) { 

    CheckBox PlayGroupSelected = (CheckBox)group_row.findViewById(R.id.bookmark_group_checkBox);   

    if (mapp.group_bookmark_s_checked.containsKey((groupPosition))==true)    
     PlayGroupSelected.setChecked(true); 
    else 
     PlayGroupSelected.setChecked(false); 
    PlayGroupSelected.setOnCheckedChangeListener(new BookmarkGroupCheckedChangeListener(groupPosition, mapp, group_row)); 

現在您的聽衆通過刪除和添加到您的狀態對象管理的狀態。我用notifyDataSetChanged(),因爲孩子們不刷新一次的檢查狀態變化繪製。不知道如果這是解決問題的正確方法,但它的伎倆。

class BookmarkGroupCheckedChangeListener implements OnCheckedChangeListener { 

    private int mPosition; 
    MyApplication mapp; 
    RelativeLayout mgroup_row; 


    BookmarkGroupCheckedChangeListener(int position, MyApplication app, RelativeLayout group_row) { 
     mapp = app; 
     mPosition = position; 
     mgroup_row = group_row; 
    } 

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

    if (isChecked == true) { 
     mapp.group_bookmark_s_checked.put((mPosition), new Boolean(true)); 
     Log.v("DEBUG:", "Group Bookmark Checked On at " + mPosition); 
     notifyDataSetChanged(); 
    } 
    else if (mapp.group_bookmark_s_checked.containsKey(mPosition)){ 
     mapp.group_bookmark_s_checked.remove((mPosition)); 
     Log.v("DEBUG:", "Checked Off at " + mPosition); 
     notifyDataSetChanged(); 
    } 


    } 

} 
+0

是。 notifyDataSetChanged();很重要。坦克 – IndieBoy