2017-11-29 9 views
0

私はExpandableListView内のすべての子ビュー要素のチェックボックスを使用しています。一度にすべてのチェックボックスをクリアするには、ボタンclearFiltersとアダプタクラスのコードを使用しています。ボタンのクリックイベントは以下のとおりです。Clear AllボタンでExpandableListView androidのすべてのチェックボックスをオフにしませんか?

ボタンをクリックしても何も起こりません。私は、ボタンがクリックされたときにすべてのチェックボックスをクリアしたいと思います。どんな助けも大歓迎です。

ExpandableListAdapterClass:ここ

public class ExpandableListViewAdapter extends BaseExpandableListAdapter { 

    private Context context; 
    private List<String> expandableListTitle; 
    private Map<String, List<ChildViewModel>> expandableListDetail; 
    private static List<ChildViewHolder> checkedViewHolders=new ArrayList<>(); 
    static int checkedBoxesCount; 

    public ExpandableListViewAdapter(Context context, List<String> expandableListTitle, Map<String, 
      List<ChildViewModel>> expandableListDetail) { 
     this.context = context; 
     this.expandableListTitle = expandableListTitle; 
     this.expandableListDetail = expandableListDetail; 
    } 
    @Override 
    public int getGroupCount() { 
     return expandableListTitle.size(); 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return expandableListDetail.get(expandableListTitle.get(groupPosition)).size(); 
    } 

    @Override 
    public String getGroup(int groupPosition) { 
     return expandableListTitle.get(groupPosition); 
    } 

    @Override 
    public ChildViewModel getChild(int groupPosition, int childPosition) { 
     return expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition); 
    } 

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

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

    @Override 
    public boolean hasStableIds() { 
     return false; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) { 
     String listTitle=getGroup(groupPosition); 
     GroupViewHolder groupViewHolder; 
     if(view==null){ 
      LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view=inflater.inflate(R.layout.expanded_list_group,null); 
      groupViewHolder=new GroupViewHolder(); 
      groupViewHolder.listTitleTextView=(TextView)view.findViewById(R.id.txtExpandedListTitle); 
      view.setTag(groupViewHolder); 
     }else { 
      groupViewHolder=(GroupViewHolder)view.getTag(); 
     } 
     groupViewHolder.listTitleTextView.setText(listTitle); 
     return view; 
    } 

    @Override 
    public View getChildView(final int groupPosition, final int childPosition, boolean b, View view, ViewGroup viewGroup) { 
     final ChildViewModel expandedListText=getChild(groupPosition,childPosition); 
     final ChildViewHolder childViewHolder; 
     if(view==null){ 
      LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view=inflater.inflate(R.layout.expanded_list_item,null); 
      childViewHolder=new ChildViewHolder(); 
      childViewHolder.expandedListTextView=(TextView)view.findViewById(R.id.txtExpandedListItem); 
      childViewHolder.checkBox=(CheckBox)view.findViewById(R.id.expandeditem_chkbox); 
      view.setTag(childViewHolder); 
     }else { 
      childViewHolder=(ChildViewHolder)view.getTag(); 
     } 
     childViewHolder.expandedListTextView.setText(expandedListText.getName()); 
     if(expandedListText.isCheckStatus()){ 
      childViewHolder.checkBox.setChecked(true); 
     }else { 
      childViewHolder.checkBox.setChecked(false); 
     } 

     childViewHolder.checkBox.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       ChildViewModel model; 
       if(childViewHolder.checkBox.isChecked()){ 
        checkedBoxesCount++; 
        model=expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition); 
        model.setCheckStatus(true); 
        expandableListDetail.get(expandableListTitle.get(groupPosition)).set(childPosition,model); 
        checkedViewHolders.add(childViewHolder); 
        notifyDataSetChanged(); 
        Toast.makeText(context,"Checked value is"+expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition),Toast.LENGTH_SHORT).show(); 
       }else { 
        checkedBoxesCount--; 
        if(checkedBoxesCount==0){ 
         Toast.makeText(context,"nothing checked",Toast.LENGTH_SHORT).show(); 
        }else { 
         Toast.makeText(context,"unchecked",Toast.LENGTH_SHORT).show(); 
        } 
        model=expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition); 
        model.setCheckStatus(false); 
        checkedViewHolders.remove(childViewHolder); 
        expandableListDetail.get(expandableListTitle.get(groupPosition)).set(childPosition,model); 
        notifyDataSetChanged(); 
       } 
      } 
     }); 
     return view; 
    } 

    public void clearChecks(){ 
     for(int i=0;i<checkedViewHolders.size();i++){ 
      checkedViewHolders.get(i).checkBox.setChecked(false); 
      notifyDataSetChanged(); 
     } 
     checkedViewHolders.clear(); 
    } 

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

    public class GroupViewHolder { 
     TextView listTitleTextView; 
    } 

    public class ChildViewHolder { 
     TextView expandedListTextView; 
     CheckBox checkBox; 
    } 
} 

はMainActivityクラスです:

public class MainActivity extends AppCompatActivity { 

    ExpandableListView mExpandableListView; 

    ExpandableListViewAdapter mExpandableListAdapter; 
    Button clearFilters; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mExpandableListView=(ExpandableListView)findViewById(R.id.expandedListView); 
     clearFilters=(Button)findViewById(R.id.btnClearFilter); 


     mExpandableListAdapter=new ExpandableListViewAdapter(MainActivity.this,getTitles(),getNames()); 
     mExpandableListView.setAdapter(mExpandableListAdapter); 

     clearFilters.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       mExpandableListAdapter.clearChecks(); 
      } 
     }); 
    } 
} 

答えて

1

動機は単純にすべてのチェックボックスをオフにしている場合、我々はハッシュマップを反復処理するためにこのテクニックを使用し、すべての確認を設定することができますstatusをfalseに設定します。すべてのchildViewのすべてのチェックボックスをクリアします。

public void clearChecks(){ 
     for (List<ChildViewModel> value:expandableListDetail.values()) { 
      for(ChildViewModel sample:value){ 
       sample.setCheckStatus(false); 
      } 
     } 
     notifyDataSetChanged(); 
+0

オハイオ州この点。 –

関連する問題