Arn
Arn

Reputation: 600

Android ExpandableListView getChildView makes a mess of data

I'm trying to learn Android and java and I'm running into difficulty with ExpandableListViews.

I am trying to make an Expandablelistview that has a number of editTexts and textViews underneath it. I could have anywhere from 1 to 20 groups and each group has 3 editTexts and a textView.

I want to populate the data from database on start up and as the users modify the data, save it back. I tried to do it but the data seems to be ALL over the place. Simple test:

-> result is that group 1 and 2 will have identical data as the last group!!! It's driving me crazy! Can someone tell me what I'm doing wrong, please?

This is my getChildView, which is very very simple.

public View getChildView(final int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {


    if (convertView == null) {
        ViewHolder holder = new ViewHolder();
        Log.i("Main", "Creating new viewHolder for " + groupPosition+childPosition);
        convertView = minflater.inflate(R.layout.childrow, null);

        holder.text1 = (EditText) convertView.findViewById(R.id.rpsinSet);
        holder.text2 = (EditText) convertView.findViewById(R.id.wtForSet);
        holder.text3 = (EditText) convertView.findViewById(R.id.rstForSet);
        holder.No    = (TextView) convertView.findViewById(R.id.NoTextView);

        holder.No.setText("Set " + (childPosition + 1));
        holder.text1.setText(""+Sets.get(groupPosition).get(childPosition).getRps());
        holder.text2.setText(""+Sets.get(groupPosition).get(childPosition).getWt());
        holder.text3.setText(""+Sets.get(groupPosition).get(childPosition).getRst());


        convertView.setTag(holder);
    } else {
        Log.i("Main", "Recovering viewHolder for " + groupPosition+childPosition);
        ViewHolder holder = (ViewHolder) convertView.getTag();
        holder.text1   = (EditText) convertView.findViewById(R.id.rpsinSet);
        holder.text2   = (EditText) convertView.findViewById(R.id.wtForSet);
        holder.text3   = (EditText) convertView.findViewById(R.id.rstForSet);
        holder.No      = (TextView) convertView.findViewById(R.id.NoTextView);
    }
    return convertView;
}

This is my ViewHolder:

static class ViewHolder {
    EditText text1;
    EditText text2;
    EditText text3;
    TextView No;
}

I did not add the database calls because they are irrelevant. I can't even get this to work right now.

Upvotes: 0

Views: 294

Answers (1)

JamesRGNT
JamesRGNT

Reputation: 616

I would like to post this as a comment but I don't have enough reputation. Anyway I'll try NOT to check if convertView is null , but I'll always recreate it, because every time you expand or close a group the list gets recreated, and maybe it's reusing the same view for every cell, and that's probably the cause of you having the same data in different lines.

Upvotes: 1

Related Questions