Marcus Gabilheri
Marcus Gabilheri

Reputation: 1289

Child Position expandable list view returning wrong value?

I'm trying to have an ExpandabelistView with 3 different layouts.

The structure is this: If the is the first layout the view to load should be firstview.xml and them if it's a even child should load even view and if is odd should load another one.

The problem is that it works sometimes but sometimes it doesn't. I tried to Log the childPosition but it gets me different values every time. The child position values returns to 0 within the same child.

Here's the code of my getChildView()

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

    final String children = (String) getChild(groupPosition, childPosition);

    TextView textTitle;
    TextView textDescription;

    if(convertView == null) {
        if(childPosition == 0) {
            convertView = inflater.inflate(R.layout.listrow_first_element, null);
        } else if(childPosition % 2 != 0) {
            convertView = inflater.inflate(R.layout.listrow_details, null);
        } else {
            convertView = inflater.inflate(R.layout.listrow_details_color2, null);
        }
    }

    textTitle = (TextView) convertView.findViewById(R.id.titleView);
    textDescription = (TextView) convertView.findViewById(R.id.descriptionView);
    textTitle.setText(getChildString(childPosition));
    textDescription.setText(children);

    return convertView;
}

And here is the Logcat that the children positions returns to me:

    03-04 19:26:10.181  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0
    03-04 19:26:10.181  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 1
    03-04 19:26:10.181  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 2
    03-04 19:26:10.191  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 3
    03-04 19:26:10.191  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 4
    03-04 19:26:10.191  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 5
    03-04 19:26:10.191  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 6
    03-04 19:26:10.201  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 7
    03-04 19:26:10.201  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0
    03-04 19:26:10.211  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 1
    03-04 19:26:10.211  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 2
    03-04 19:26:10.221  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 3
    03-04 19:26:10.221  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 4
    03-04 19:26:10.221  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 5
    03-04 19:26:14.876  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4
    03-04 19:26:14.876  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32409816 bytes)
    03-04 19:26:15.096  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 6
    03-04 19:26:16.207  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc
    03-04 19:26:19.601  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4
    03-04 19:26:19.601  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32414792 bytes)
    03-04 19:26:19.661  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 7
    03-04 19:26:20.001  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 1
    03-04 19:26:20.121  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0
    03-04 19:26:20.952  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc
    03-04 19:26:21.032  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4
    03-04 19:26:21.032  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32416096 bytes)
    03-04 19:26:21.152  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc
    03-04 19:26:23.114  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] DisableGcInRange: toleranceRange = 4
    03-04 19:26:23.114  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] disableGcInRange: Left 4194304 bytes to trigger GC (current size:32416096 bytes)
    03-04 19:26:23.154  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 6
    03-04 19:26:23.214  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 7
    03-04 19:26:23.555  23283-23283/com.gabilheri.insuringmylife D/Child Position:﹕ 0
    03-04 19:26:23.645  23283-23283/com.gabilheri.insuringmylife D/dalvikvm﹕ [GC Control] nativeRestoreGc

Upvotes: 2

Views: 1292

Answers (1)

Cruceo
Cruceo

Reputation: 6834

This is your problem:

if(convertView == null) {
    if(childPosition == 0) {
        convertView = inflater.inflate(R.layout.listrow_first_element, null);
    } else if(childPosition % 2 != 0) {
        convertView = inflater.inflate(R.layout.listrow_details, null);
    } else {
        convertView = inflater.inflate(R.layout.listrow_details_color2, null);
    }
}

convertView won't always be null when getView() is called, and will not always be the same. The ListView's Adapter will reuse View's as it sees fit and it has no way of distinguishing between your types of Views.

Your solutions are to either:

A. Inflate the View every time (more costly than a regular type)

B. Pre-inflate the Views then save them in the object that backs your adapter, and return them when calling getView() (potentially more or less costly type A)

C. Not do what you're trying to do (but this is a dumb option, so forget this one)

I would probably go with A unless you see heavy performance issues.

Upvotes: 2

Related Questions