views:

42

answers:

1

I need to add a header to the top of the expanded children in an ExpandableListView. The same header should appear at the top of all expanded children. I've been trying to do it from the adapter like this:

public int getChildrenCount(int groupPosition) {
 //Add one extra to children size
 return mGroups.get(groupPosition).size()+1;
}



 public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    //if childPosition 0 return the header
    if(childPosition == 0)return convertView = mInflater.inflate(R.layout.child_header, null);

    //childPosition -1 is the actual item
    ChildItem childItem = (ChildItem) getChild(groupPosition, childPosition-1);
    ChildViewHolder holder = null;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.child_row, null);
    } else {
    holder = (ChildViewHolder) convertView.getTag();
    }

    if(holder == null){
        holder = new ChildViewHolder(convertView);
        convertView.setTag(holder);
    }

    holder.getTextLabel().setText(childItem.name);
    holder.getPriceLabel().setText(String.valueOf(childItem.price));

    return convertView;
}

ChildViewHolder handles finding Views in the child row. This seems to work but then randomly convertView will be the R.layout.child_header at childPosition other than 0. This then breaks getTextLabel() call.

Is there a better way of doing this? what I really want is something like this:

myExpandableListView.addChildHeaderView(new ChildHeader())

I've solved the problem by creating the view like this instead of as above

if(convertView!=null){
    holder = (ChildViewHolder) convertView.getTag();
 }


if(holder == null){
    convertView = mInflater.inflate(R.layout.child_row, null);
    holder = new ChildViewHolder(convertView);
    convertView.setTag(holder);
}

Thanks!

A: 

Hi Sameer,

Sorry for the late reply, here is my complete code

public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
            if(childPosition == 0)return convertView = mInflater.inflate(R.layout.child_header, null);

            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            ChildViewHolder holder = null;

            ChildItem childItem = (ChildItem) getChild(groupPosition, childPosition-1);

            //Get ViewHolder first
            if(convertView!=null){
                holder = (ChildViewHolder) convertView.getTag();
            }

            //If no ViewHolder, then create a new child row as convertView is probably a header
            if(holder == null){
                convertView = mInflater.inflate(R.layout.child_row, null);
                holder = new ChildViewHolder(convertView);
                convertView.setTag(holder);
            }
            holder.getTextLabel().setText(childItem.name);
            holder.getPriceLabel().setText(String.valueOf(childItem.price));
            return convertView;
        }
longhairedsi