1

I have implemented an ExpandableListView and a BaseExpandableListAdapter. I want to display a list of GroupObjects. Every GroupObject (Top-items in ExpandableList) has an ArrayList of 3 ChildObjects (Child-items in ExpandableList).

I am using the viewHolder-pattern. The GroupObjects are displayed correctly. Once I expand the first GroupObject, getChildView() gets called 3 times and displays its ChildObjects correctly.

But as soon as I expand another GroupObject, getChildView() returns the ChildObjects of the first GroupObject. Since the convertView of the second call is not null, it always returns the same ChildObjects.

Here is some Code:

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
        View convertView, ViewGroup parent) {
    final ChildObject childobject = (ChildObject)getChild(groupPosition, childPosition);
    ChildViewholder childViewholder;

    if (convertView == null) {
        childViewholder = new ChildViewholder();
        LayoutInflater inflater = (LayoutInflater)context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (!childobject.isReserved()) {
            convertView = inflater.inflate(R.layout.childobject_free,
                    parent, false);
            childViewHolder.layout = (RelativeLayout)convertView
                    .findViewById(R.id.childobject_free_layout);

        } else {
            final Reservation reservation = childobject.getReservation();
            convertView = inflater.inflate(
                    R.layout.childobject_reserved, parent, false);
            childViewHolder.layout = (RelativeLayout)convertView.findViewById(R.id.reserved_layout);

            childViewHolder.reservationImage = (ImageView)convertView.findViewById(R.id.reservationImage);
            childViewHolder.reservationName = (TextView)convertView.findViewById(R.id.reservationName);
            childViewHolder.buttonFinish = (ImageButton)convertView
                    .findViewById(R.id.finishButton);
            childViewHolder.buttonCancel = (ImageButton)convertView
                    .findViewById(R.id.cancelButton);
            childViewHolder.progressBar = (ProgressBar)convertView
                    .findViewById(R.id.reservationProgress);

            childObject.getTimer().setProgressBar(childViewHolder.progressBar);
            childViewHolder.reservationName.setText(reservation.getName());
            childViewHolder.reservationImage.setImageBitmap(reservation.getBitmap());

            childViewHolder.buttonFinish.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    DrawerClickHandler.finishReservation(reservation);
                }
            });

            childViewHolder.buttonCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DrawerClickHandler.cancelReservation(reservation);
                }
            });

        }

        convertView.setTag(childViewholder);

    }else{
        childViewholder = (ChildViewholder)convertView.getTag();
    }

        childViewHolder.groupPosition = groupPosition;
        childViewHolder.childPosition = childPosition;

        return convertView;
}

The convertView is only null on the first 3 calls of getChildView().

Let me know if you need more code.

Any help is appreciated.

Martin Golpashin
  • 1,032
  • 9
  • 28
  • Can you post your full code for `getChildView`? – Abhishek V Dec 29 '13 at 03:19
  • It is basically the full code. I just left out the part where I set the attributes for the components of childViewHolder – Martin Golpashin Dec 29 '13 at 05:11
  • Yeah..that part is important – Abhishek V Dec 29 '13 at 05:14
  • ok I edited it. let me know if you need any further details. – Martin Golpashin Dec 29 '13 at 14:36
  • 1
    First of all, you're setting the data to the child view **only** when the convert view is `null`(so the data needs to be set outside of the `if` condition testing for a null convert view), when the convert view is not null you end up with data from previous rows. Secondly, that is not how you handle two types of rows in an `ExpandableListView`. – user Dec 29 '13 at 14:55
  • @MartinGolpashin @Luksprog is right. You are not setting the data when `convertView` is `not null`. And that is not the way to handle two types of rows. You need to make use of `getChildType()` and `getChildTypeCount()` of `BaseExpandableListAdapter` class. – Abhishek V Dec 29 '13 at 16:39

0 Answers0