GetGroupView (), on the second level, expandableListView, always has the same group arrangement to all elements of the second level

I have two ExpandableListViews, one inside the other containing shifts. The first level list groups all shifts that have a specific year, and the second level list has the same function as the first level list, but represents the months of the parent year.

Each time getGroupView is called on the MonthListAdapter, groupPosition is the same. This means that every second level name is the same for all second level elements when I call:

TextView title = (TextView) convertView.findViewById(R.id.monthTitle);
    title.setText("" + (_shiftMonths.keyAt(groupPosition) + 1));

Am I using groupPosition correctly? If not, how to get the position / identifier of the second level position?

Also getGroupView () in MonthListAdapter calls for some reason twice for each element.

YearListAdapter: - contains elements corresponding to a specific year

package com.dg.android.salarycalculator.adapter;

import java.util.ArrayList;
import java.util.Calendar;

import com.dg.android.salarycalculator.R;
import com.dg.android.salarycalculator.Salary;
import com.dg.android.salarycalculator.views.MonthsListView;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class YearListAdapter extends BaseExpandableListAdapter {

private Context _context;
private SparseArray<SparseArray<ArrayList<Salary>>> _shiftYears;

public YearListAdapter(
        SparseArray<SparseArray<ArrayList<Salary>>> shiftYears,
        Context context) {
    super();

    _context = context;
    _shiftYears = shiftYears;

}

public Object getChild(int arg0, int arg1) {
    return (null == _shiftYears.get(_shiftYears.keyAt(arg0))) ? null
            : _shiftYears.get(_shiftYears.keyAt(arg0)).get(
                    _shiftYears.get(_shiftYears.keyAt(arg0)).keyAt(arg1));
}

public long getChildId(int arg0, int arg1) {
    return arg1;
}

public View getChildView(int arg0, int arg1, boolean arg2,
        View convertView, ViewGroup arg4) {

    MonthsListView monthLevelExpandable = new MonthsListView(_context);

    MonthListAdapter adapter = new MonthListAdapter(_context,
            _shiftYears.get(_shiftYears.keyAt(arg0)));
    monthLevelExpandable.setAdapter(adapter);

    adapter.forceReload();
    monthLevelExpandable.setGroupIndicator(null);

    return monthLevelExpandable;

}

public int getChildrenCount(int arg0) {
    if (null == _shiftYears.get(_shiftYears.keyAt(arg0)))
        return 0;
    return _shiftYears.get(_shiftYears.keyAt(arg0)).size();
}

public Object getGroup(int arg0) {
    return (null == _shiftYears) ? null : _shiftYears.get(_shiftYears
            .keyAt(arg0));
}

public int getGroupCount() {
    return _shiftYears.size();
}

public long getGroupId(int arg0) {
    return arg0;
}

public View getGroupView(int arg0, boolean isExpanded, View convertView,
        ViewGroup arg3) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) _context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.year_list_view_item, null);

    }

    TextView title = (TextView) convertView.findViewById(R.id.yearTitle);
    title.setText("" + _shiftYears.keyAt(arg0));

    return convertView;

}

public boolean hasStableIds() {
    return true;
}

public boolean isChildSelectable(int arg0, int arg1) {
    return true;
}

public void forceReload() {
    notifyDataSetChanged();
}

}

MonthListAdapter: - contains elements corresponding to a specific month

package com.dg.android.salarycalculator.adapter;

import java.util.ArrayList;
import java.util.Calendar;

import com.dg.android.salarycalculator.R;
import com.dg.android.salarycalculator.Salary;
import com.dg.android.salarycalculator.views.ShiftListItem;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MonthListAdapter extends BaseExpandableListAdapter {

private Context _context;
private SparseArray<ArrayList<Salary>> _shiftMonths;

public MonthListAdapter(Context context,
        SparseArray<ArrayList<Salary>> shiftMonths) {
    super();

    _context = context;
    _shiftMonths = shiftMonths;

}

public Object getChild(int groupPosition, int childPosition) {
    return (null == _shiftMonths) ? null : _shiftMonths.get(
            _shiftMonths.keyAt(groupPosition)).get(childPosition);

}

public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

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


    ShiftListItem sli;
    if (null == convertView) {
        sli = (ShiftListItem) View.inflate(_context,
                R.layout.shift_list_item, null);
    } else {
        sli = (ShiftListItem) convertView;
    }
    sli.setSalary(_shiftMonths.get(_shiftMonths.keyAt(groupPosition)).get(
            childPosition));
    return sli;
}





public int getChildrenCount(int groupPosition) {
    if (null == _shiftMonths
            || null == _shiftMonths.get(_shiftMonths.keyAt(groupPosition)))
        return 0;           
    return _shiftMonths.get(_shiftMonths.keyAt(groupPosition)).size();
}

public Object getGroup(int groupPosition) {
    return (null == _shiftMonths) ? null : _shiftMonths.get(_shiftMonths
            .keyAt(groupPosition));
}

public int getGroupCount() {
    return _shiftMonths.size();
}

public long getGroupId(int groupPosition) {
    return groupPosition;
}

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

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) _context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.month_list_view_item, null);
    }

    TextView title = (TextView) convertView.findViewById(R.id.monthTitle);
    title.setText("" + _shiftMonths.keyAt(groupPosition));

    return convertView;

}

public boolean hasStableIds() {
    return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public void forceReload() {
    notifyDataSetChanged();
}

}
0
source share
1 answer

I made a novice user error and did not pass childID to getChildView(...)in the class . This problem was resolved (hopefully correctly) by adding the following command to : YearListAdaptergetChildView(...)

monthLevelExpandable.setSelectedGroup(arg1);( arg1 is the child position passed in the parameters).

+3
source

All Articles