lundi 29 octobre 2018

BaseExpandableListAdapter rewrites new items over the old items

I am using BaseExpandableListAdapter to show titles with children. The children can be a Checkbox or a radiobutton, it depends of the parent of each one, so you can have a group which are radiobuttons or a group which are checkboxes.

The problem is that when I'm scrolling, the items are been override bad, and you can see the old element behind the new one. I have tried to use getChildId and getGroupId with autoincrements uniques ID, to use getCombinedGroupId and getCombinedChildId, I have set the hasStableIds true and false. But it still hapenning.

This is a piece of my Adapter class:

private Context context;
private ArrayList<Mode> parents;
private HashMap<Mode,ArrayList<Mode>> children;
private ArrayList<RadioButton> radioButtonList;
private byte idLang;
private ModeDao modeDao;
private ExpandableListView elOrderDialog;
private TextView title;
private Timer errorTimer;

public NewModesAdapter(Context context, ArrayList<Mode> parents, HashMap<Mode, ArrayList<Mode>> children, byte idLang,ExpandableListView elOrderDialog, TextView title) {
    this.context = context;
    this.parents = parents;
    this.children = children;
    this.idLang = idLang;
    this.modeDao = new ModeDao(context);
    this.elOrderDialog = elOrderDialog;
    this.title = title;
    this.radioButtonList = new ArrayList<>();
    this.errorTimer = new Timer();
}

@Override
public void registerDataSetObserver(DataSetObserver observer) {

}

@Override
public void unregisterDataSetObserver(DataSetObserver observer) {

}

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

@Override
public int getChildrenCount(int groupPosition) {
    return children.get(parents.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return this.parents.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return this.children.get(this.parents.get(groupPosition))
            .get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return this.parents.get(groupPosition).getAutoId();
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return this.children.get(this.parents.get(groupPosition))
            .get(childPosition).getAutoId();
}

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    Mode currentFilterParent = (Mode) getGroup(groupPosition);
    String headerTitle = currentFilterParent.getTranslationName(idLang);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert inflater != null;
        convertView = inflater.inflate(R.layout.expandable_title, null);
    }

    FrameLayout flTitle = convertView.findViewById(R.id.fl_title);
    flTitle.setBackgroundColor(Color.parseColor(AppData.getColor2(context)));
    final TextView lblListHeader = convertView.findViewById(R.id.ex_tv_title);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setTextColor(Color.parseColor(AppData.getText2(context)));
    lblListHeader.setText(headerTitle);

    flTitle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //nothing to do
        }
    });

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    CheckBox cbModeItem;
    RadioButton rbModeItem;
    final Mode currentChild = (Mode) getChild(groupPosition, childPosition);
    Mode currentParent = modeDao.getModeById(currentChild.getIdFather());

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert inflater != null;
        convertView = inflater.inflate(R.layout.expandable_mode_item, null);
    }

    if (currentParent.getMultiOption() == 0) {
        rbModeItem = convertView.findViewById(R.id.rb_mode_item);
        rbModeItem.setText(currentChild.getTranslationName(idLang));
        rbModeItem.setTextColor(Color.parseColor(AppData.getText1(context)));
        rbModeItem.setVisibility(View.VISIBLE);
        setRBOnClick(rbModeItem, currentChild);
        radioButtonList.add(rbModeItem);
    } else {
        cbModeItem = convertView.findViewById(R.id.cb_mode_item);
        cbModeItem.setText(currentChild.getTranslationName(idLang));
        cbModeItem.setTextColor(Color.parseColor(AppData.getText1(context)));
        cbModeItem.setVisibility(View.VISIBLE);
        cbModeItem.setChecked(currentChild.isChecked());
        setCBOnClick(cbModeItem,currentChild);
    }

    return convertView;
}

Thank you in advance.




Aucun commentaire:

Enregistrer un commentaire