vendredi 9 décembre 2016

ExpandableListView is not remembering checkboxes state

My ExpandableListView is not saving children checkboxes state, so when a checked checkbox goes out of the viewport and back again, it became unchecked again. Or when I click on another parent the same thing happens.

Here's my code of the ExpandableListAdapter class:

public class AffiliatorExpandableAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<String> mParents;
    private List<List<String>> mChildren;
    private Set<Pair<String, String>> checkedItems;

    public AffiliatorExpandableAdapter(Context context, List<String> parents, List<List<String>> children)
    {
        this.mContext = context;
        this.mParents = parents;
        this.mChildren = children;
        this.checkedItems = new HashSet<>();
    }

    @Override
    public int getGroupCount() {
        return this.mParents.size();
    }

    @Override
    public int getChildrenCount(int i) {
        return this.mChildren.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return this.mParents.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return this.mChildren.get(i).get(i1);
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view1 =  inflater.inflate(R.layout.textview_item, null);
        TextView groupName = (TextView) view1.findViewById(R.id.textview_item);
        groupName.setText(this.mParents.get(i));
        return view1;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final String parentName = this.mParents.get(i);
        final String childName = this.mChildren.get(i).get(i1);
        final Pair<String, String> selection = new Pair<>(parentName, childName);
        View view1 =  inflater.inflate(R.layout.checked_textview_item, null);
        final CheckedTextView childItem = (CheckedTextView) view1.findViewById(R.id.checked_textview_item);
        childItem.setText(childName);
        childItem.setChecked(checkedItems.contains(selection));
        childItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(((CheckedTextView) view).isChecked()) {
                    ((CheckedTextView) view).setChecked(false);
                    checkedItems.remove(selection);
                } else {
                    ((CheckedTextView) view).setChecked(true);
                    checkedItems.add(selection);
                }
            }
        });

        return view1;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}

textview_item layout:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/textview_item"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="15dp"
    android:textColor="@color/md_black_1000"/>

checked_textview_item layout:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://ift.tt/nIICcg"
    android:id="@+id/checked_textview_item"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/textCheckMarkInverse"
    android:textColor="@color/md_black_1000"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:focusable="false"
    android:focusableInTouchMode="false"/>

Aucun commentaire:

Enregistrer un commentaire