I have an ExpandableListView
. And when I click on groupItem
it goes to another page, that is, I do replace Fragment
. And at child Item
oh checkbox
. I switch back and forth when I click back or even when I open this page all checked CheckBox
erased.
My adapter:
public class RubExpandAdapter extends BaseExpandableListAdapter { private Context context; private List<RubricsModel> rubrics; private List<VacancyModel> listForRubricListView; private List<VacancyModel> listForSubRubricListView; private HashMap<Integer, boolean[]> mChildCheckStates; public static int rubricPage = 1; public static int rubricId; public static int subRubricPage = 1; public static int subRubricId; FragmentManager fm; public RubExpandAdapter(Context context, ArrayList<RubricsModel> rubrics, FragmentManager fragmentManager) { this.context = context; this.rubrics = rubrics; fm = fragmentManager; listForRubricListView = new ArrayList<>(); mChildCheckStates = new HashMap<Integer, boolean[]>(); } @Override public Object getChild(int groupPosition, int childPosition) { ArrayList<SubRubricsModel> chList = rubrics.get(groupPosition) .getItems(); return chList.get(childPosition); } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final int mGroupPosition = groupPosition; final int mChildPosition = childPosition; SubRubricsModel subRubricsModel = (SubRubricsModel) getChild(groupPosition, childPosition); if (convertView == null) { LayoutInflater infalInflater = (LayoutInflater) context .getSystemService(context.LAYOUT_INFLATER_SERVICE); convertView = infalInflater.inflate(R.layout.rubrics_child_row_layout, null); } final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox); TextView tv = (TextView) convertView.findViewById(R.id.tvsubrubricsfr); tv.setText(subRubricsModel.getSubRubricName()); convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { checkBox.setChecked(!checkBox.isChecked()); } }); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { boolean getChecked[] = mChildCheckStates.get(mGroupPosition); getChecked[mChildPosition] = isChecked; mChildCheckStates.put(mGroupPosition, getChecked); } else { boolean getChecked[] = mChildCheckStates.get(mGroupPosition); getChecked[mChildPosition] = isChecked; mChildCheckStates.put(mGroupPosition, getChecked); } } }); if (mChildCheckStates.containsKey(mGroupPosition)) { boolean getChecked[] = mChildCheckStates.get(mGroupPosition); checkBox.setChecked(getChecked[mChildPosition]); } else { boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)]; mChildCheckStates.put(mGroupPosition, getChecked); checkBox.setChecked(false); } return convertView; } @Override public int getChildrenCount(int groupPosition) { ArrayList<SubRubricsModel> chList = rubrics.get(groupPosition) .getItems(); return chList.size(); } @Override public Object getGroup(int groupPosition) { return rubrics.get(groupPosition); } @Override public int getGroupCount() { return rubrics.size(); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { final RubricsModel rubricsModel = (RubricsModel) getGroup(groupPosition); if (convertView == null) { LayoutInflater inf = (LayoutInflater) context .getSystemService(context.LAYOUT_INFLATER_SERVICE); convertView = inf.inflate(R.layout.rubrics_group_row_layout, null); } TextView tv = (TextView) convertView.findViewById(R.id.tvrubricsfr); tv.setText(rubricsModel.getRubricName()); ImageView indicator = (ImageView) convertView.findViewById(R.id.ivGroupIndicator); if (isExpanded) { indicator.setImageResource(R.drawable.arrow_down); } else { indicator.setImageResource(R.drawable.arrow_right); } tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { RubFragment.searchByRubrics = true; rubricId = rubricsModel.getRubricID(); searchByRubric(String.valueOf(rubricId)); } }); return convertView; } }
Question: How to save the state of Checkbox
and how to calculate the number of checked checkBox
?