getGroupView(), in the second level expandableListView, always has the same groupPosition to all the second level items

StackOverflow https://stackoverflow.com/questions/16739690

Frage

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

Every time getGroupView on MonthListAdapter is being called, the groupPosition is the same. Meaning that every second level item's name is the same, across all the second level items, 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 do I get the second level item position/id?

Also getGroupView() on the MonthListAdapter is being called twice for each item for some reason.

YearListAdapter: - has items 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:- has items 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();
}

}
War es hilfreich?

Lösung

I've made a novice mistake and didn't pass the childID at getChildView(...) in the YearListAdapter class. This issue has been solved (hopefully correctly) by adding the next command at getChildView(...):

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top