mardi 3 mai 2016

doesn't save checkbox states in expandablelistview after reopen dialog

I display expandablelistview in dialog in order to choose categories of item. When first open dialog i get normally selected elements and put to edittext. When second and more reopen dialog doesn't save checkbox states of selected elements. Also when toggle groups, changed selected checkbox. I don't know why this occured? Can anybody help? I offer my code, following:

public class CategoryEditText<T extends Listable> extends EditText {
    List<T> mItems;
    String[] mListableItems;
    CharSequence mHint;
    OnItemSelectedListener<T> onItemSelectedListener;
    ExpandableListView expandableListView;
    View customView;
    CategoryAdapter categoryAdapter;

    public CategoryEditText(Context context) {
        super(context);
        mHint = getHint();
    }

    public CategoryEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mHint = getHint();
    }

    public CategoryEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mHint = getHint();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CategoryEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mHint = getHint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        setFocusable(false);
        setClickable(true);
    }

    public void setItems(List<T> items) {
        this.mItems = items;
        customView = LayoutInflater.from(getContext()).inflate(R.layout.select_category_layout, null);
        expandableListView = (ExpandableListView) customView.findViewById(R.id.list);
        categoryAdapter = new CategoryAdapter(getContext(), (ArrayList<Category>) mItems);
        expandableListView.setAdapter(categoryAdapter);
        this.mListableItems = new String[items.size()];

        int i = 0;

        for (T item : mItems) {
            mListableItems[i++] = item.getLabel();
        }

        configureOnClickListener();
    }

    private void configureOnClickListener() {
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                MaterialDialog dialog = new MaterialDialog.Builder(getContext())
                        .title(R.string.shop_category)
                        .customView(customView, false)
                        .positiveText(R.string.select)
                        .negativeText(android.R.string.cancel)
                        .onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog materialDialog, @NonNull DialogAction dialogAction) {
                                ArrayList<Category> cats = categoryAdapter.getSelectedCats();
                                String selectedText = "";
                                for(int i=0; i<cats.size(); i++){
                                    selectedText += cats.get(i).getTitle();
                                    if(i+1 < cats.size()){
                                        selectedText += " | ";
                                    }
                                }
                                setText(selectedText);
                                materialDialog.dismiss();
                            }
                        })
                        .build();
                dialog.show();
            }
        });
    }

    public void setOnItemSelectedListener(OnItemSelectedListener<T> onItemSelectedListener) {
        this.onItemSelectedListener = onItemSelectedListener;
    }

    public interface OnItemSelectedListener<T> {
        void onItemSelectedListener(T item, int selectedIndex);
    }
}

In my adapter class

public class CategoryAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Category> categories;
    private LayoutInflater inflater;

    public CategoryAdapter(Context context,
                           ArrayList<Category> categoryList) {
        this.context = context;
        this.categories = categoryList;
        inflater = LayoutInflater.from(context);
    }

    public Object getChild(int groupPosition, int childPosition) {
        return categories.get(groupPosition).getSub_categories().get(childPosition);
    }

    public long getChildId(int groupPosition, int childPosition) {
        return (long)( groupPosition*1024+childPosition );  // Max 1024 children per group
    }

    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.category_child_row, parent, false);
        final Category c = (Category) getChild( groupPosition, childPosition );
        TextView title = (TextView)v.findViewById(R.id.title);
        title.setText(c.getTitle());
        CheckBox cb = (CheckBox)v.findViewById(R.id.check);
        if(((Category)getGroup(groupPosition)).getSub_categories().get(childPosition).isChecked()){
            cb.setChecked(true);
        } else {
            cb.setChecked(false);
        }
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    c.setChecked(true);
                } else {
                    c.setChecked(false);
                }
            }
        });
        return v;
    }

    public int getChildrenCount(int groupPosition) {
        return categories.get(groupPosition).getSub_categories().size();
    }

    public Object getGroup(int groupPosition) {
        return categories.get( groupPosition );
    }

    public int getGroupCount() {
        return categories.size();
    }

    public long getGroupId(int groupPosition) {
        return (long)( groupPosition*1024 );  // To be consistent with getChildId
    }

    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View v = null;
        if( convertView != null )
            v = convertView;
        else
            v = inflater.inflate(R.layout.category_parent_row, parent, false);
        final Category category = (Category)getGroup(groupPosition);
        TextView title = (TextView)v.findViewById(R.id.title);
        CheckBox cb = (CheckBox)v.findViewById(R.id.check);
        ImageView image = (ImageView)v.findViewById(R.id.arrow);
        title.setText(category.getTitle());
        if(getChildrenCount(groupPosition) == 0){
            image.setVisibility(View.INVISIBLE);
            cb.setVisibility(View.VISIBLE);
            if(((Category) getGroup(groupPosition)).isChecked()){
                cb.setChecked(true);
            } else{
                cb.setChecked(false);
            }
            cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                    if(isChecked) {
                        category.setChecked(true);
                    } else {
                        category.setChecked(false);
                    }
                }
            });
        } else {
            image.setVisibility(View.VISIBLE);
            cb.setVisibility(View.INVISIBLE);
            image.setImageResource(isExpanded ? R.drawable.ic_chevron_down : R.drawable.ic_chevron_right);
        }
        return v;
    }

    public boolean hasStableIds() {
        return true;
    }

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

    public void onGroupCollapsed (int groupPosition) {}
    public void onGroupExpanded(int groupPosition) {}

    public ArrayList<Category> getSelectedCats(){
        ArrayList<Category> checkedCategories = new ArrayList<>();
        for(Category c: categories){
            if(c.isChecked()) checkedCategories.add(c);
            if(!c.getSub_categories().isEmpty()){
                for(Category category: c.getSub_categories()) {
                    if (category.isChecked()) checkedCategories.add(category);
                }
            }
        }
        return checkedCategories;
    }

}




Aucun commentaire:

Enregistrer un commentaire