getGroupView(),在第二级expandableListView中,始终与所有第二级项具有相同的groupPosition

时间:2013-05-24 16:32:37

标签: android expandablelistview expandablelistadapter

我有两个ExpandableListViews,一个在另一个内部,包含shift。第一级列表将具有特定年份的所有班次分组,第二级列表与第一级列表具有相同的功能,但代表父母年份的月份。

每次调用MonthListAdapter上的getGroupView时,groupPosition都是相同的。这意味着当我打电话时,所有第二级项目的每个第二级项目的名称都是相同的:

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

我正确使用groupPosition吗?如果不是,我如何获得第二级项目位置/ ID?

由于某种原因,每个项目也会调用MonthListAdapter上的getGroupView()两次。

YearListAdapter: - 具有与特定年份相对应的项目

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: - 包含与特定月份相对应的项目

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();
}

}

1 个答案:

答案 0 :(得分:3)

我犯了一个新手错误,并且没有在 getChildView(...) 类的YearListAdapter处传递childID。通过在getChildView(...)添加下一个命令来解决此问题(希望正确):

monthLevelExpandable.setSelectedGroup(arg1); arg1 是参数中传输的子位置)。