Expandable List View adapter getView() repeats position

603 views Asked by At

My ExpandableListView's adapter repeats the position of View and Images when I scroll below is my code. I used it without View holder and with view holder in both cases I face this issue.

public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {

    View view = convertView;
    String groupName = (String) getGroup(groupPosition);

    if (view == null) {
        LayoutInflater inf = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inf.inflate(R.layout.group_item, null);
        holder = new ViewHolder();

        holder.arrowImage = (ImageView) view.findViewById(R.id.imageView1);
        setImageView(holder.arrowImage, groupPosition);
        view.setTag(holder);
        holder.groupName = (TextView) view.findViewById(R.id.laptop);
        holder.groupName.setTypeface(null, Typeface.BOLD);
        holder.groupName.setText(groupName);

    } else {
        holder = (ViewHolder) view.getTag();
        holder.groupName = (TextView) view.findViewById(R.id.laptop);
        holder.groupName.setTypeface(null, Typeface.BOLD);
        holder.groupName.setText(groupName);
    }

    return view;

}

public ImageView getImageView(int position) {
    return images[position];
}

public void setImageView(ImageView image, int position) {
    images[position] = image;
}

static class ViewHolder {
    ImageView arrowImage;
    TextView groupName;
}
1

There are 1 answers

1
sourabh devpura On

Don't pick view by tag

use this

public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {

    View view = convertView;
    String groupName = (String) getGroup(groupPosition);

    LayoutInflater inf = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inf.inflate(R.layout.group_item, null);
        holder = new ViewHolder();

        holder.arrowImage = (ImageView) view.findViewById(R.id.imageView1);
        holder.groupName = (TextView) view.findViewById(R.id.laptop);
        holder.groupName.setTypeface(null, Typeface.BOLD);
        holder.groupName.setText(groupName);
        setImageView(holder.arrowImage, groupPosition);
        view.setTag(holder);

    return view;

}

public ImageView getImageView(int position) {
    return images[position];
}

public void setImageView(ImageView image, int position) {
    images[position] = image;
}

static class ViewHolder {
    ImageView arrowImage;
    TextView groupName;
}

this will prevent image repetition and consider every view as a new view