samedi 25 février 2017

Changing group view in ExpandableListView

I have an ExpandableListView, it has checkboxes in the groups and in the childs. The checkbox in the group should be full when all it's childs are checked.

It works at the beginning when the activity first start, but if for example all of a group's childs are checked and it's checked too, and then I uncheck one of it's child - it won't uncheck.

I tried to write down that code in the getGroupView method, the same way I controlled the checkbox in each child through the getChildView method.

Where should I change the group's view after the activity has already started?

The code looks like this:

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    final String childText = (String) getChild(groupPosition, childPosition);
    final Boolean childIsWatched = (Boolean) getCheckedParameterChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.expandable_list_item_watched, null);
    }

    // Set episode's name and checkbox
    ((TextView) convertView.findViewById(R.id.list_item_header)).setText(childText);
    final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.check_box_item);
    checkBox.setChecked(childIsWatched);

    // It had to be final to be inside the checkBox.setOnClickListener method
    final View finalConvertView = convertView;

    // Set checkbox press
    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Get the new check value
            boolean newIsWatched = checkBox.isChecked();

            // Change episode in DB
            Model.getInstance().ChangeIsWatchedForEpisode(mContext,
                    mSeries.getId(),
                    Integer.toString(groupPosition + 1),
                    Integer.toString(childPosition + 1),
                    newIsWatched);

            // Change the adapter
            updateIsSeenValue(Integer.toString(groupPosition + 1),
                    Integer.toString(childPosition + 1),
                    newIsWatched);

            // Notify that there was a change
            notifyDataSetChanged();
        }
    });

    return convertView;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    // Set the group from the general ExpandableListAdapter
    View newConvertView = super.getGroupView(groupPosition, isExpanded, convertView, parent);

    // Set the group's checkboxes
    this.setGroupCheckbox(groupPosition, newConvertView);

    return newConvertView;
}




Aucun commentaire:

Enregistrer un commentaire