getGroupView(),在第二级expandableListView,总是具有相同的groupPosition所有二级项目 [英] getGroupView(), in the second level expandableListView, always has the same groupPosition to all the second level items

查看:712
本文介绍了getGroupView(),在第二级expandableListView,总是具有相同的groupPosition所有二级项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个ExpandableListViews,人在里面另外一个,包含的变化。第一个层次列表组都具有特定的一年中的变化和第二级列表具有相同的功能第一级列表,但再presents父母的一年中的月份。

上MonthListAdapter每次getGroupView正在被呼叫,则groupPosition是相同的。这意味着,每一个二级项目名称是相同的,在所有的第二个层次的项目,我的呼唤:

  TextView的标题=(TextView中)convertView.findViewById(R.id.monthTitle);
    title.setText(+(_shiftMonths.keyAt(groupPosition)+ 1));
 

我使用groupPosition是否正确?如果不是我怎么二级项目位置/ ID?

此外getGroupView()上的MonthListAdapter被调用两次每个项目的某些原因。

YearListAdapter: - 具有对应于特定年份项目

 包com.dg.android.salarycalculator.adapter;

进口的java.util.ArrayList;
进口的java.util.Calendar;

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

公共类YearListAdapter扩展BaseExpandableListAdapter {

私人语境_context;
私人SparseArray< SparseArray< ArrayList的<薪酬和GT;>> _shiftYears;

公共YearListAdapter(
        SparseArray< SparseArray< ArrayList的<薪酬和GT;>> shiftYears,
        上下文的背景下){
    超();

    _context =背景;
    _shiftYears = shiftYears;

}

公共对象getChild(INT为arg0,诠释ARG1){
    返回(空== _shiftYears.get(_shiftYears.keyAt(arg0中)))?空值
            :_shiftYears.get(_shiftYears.keyAt(arg0中))得到(
                    _shiftYears.get(_shiftYears.keyAt(arg0中))keyAt(ARG1))。
}

众长getChildId(INT为arg0,诠释ARG1){
    返回ARG1;
}

公共查看getChildView(INT为arg0,诠释ARG1,布尔ARG2,
        查看convertView,ViewGroup中ARG4){

    MonthsListView monthLevelExpandable =新MonthsListView(_context);

    MonthListAdapter适配器=新MonthListAdapter(_context,
            _shiftYears.get(_shiftYears.keyAt(arg0中)));
    monthLevelExpandable.setAdapter(适配器);

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

    返回monthLevelExpandable;

}

公众诠释getChildrenCount(INT为arg0){
    如果(空== _shiftYears.get(_shiftYears.keyAt(arg0中)))
        返回0;
    返回_shiftYears.get(_shiftYears.keyAt(arg0中))的大小()。
}

公共对象getGroup(INT为arg0){
    返回(空== _shiftYears)?空:_shiftYears.get(_shiftYears
            .keyAt(arg0中));
}

公众诠释getGroupCount(){
    返回_shiftYears.size();
}

众长getGroupId(INT为arg0){
    返回将arg0;
}

公共查看getGroupView(INT为arg0,布尔isExpanded,查看convertView,
        ViewGroup中ARG3){

    如果(convertView == NULL){
        LayoutInflater充气=(LayoutInflater)_context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.year_list_view_item,NULL);

    }

    TextView的标题=(TextView中)convertView.findViewById(R.id.yearTitle);
    title.setText(+ _shiftYears.keyAt(arg0中));

    返回convertView;

}

公共布尔hasStableIds(){
    返回true;
}

公共布尔isChildSelectable(INT为arg0,诠释ARG1){
    返回true;
}

公共无效forceReload(){
    notifyDataSetChanged();
}

}
 

MonthListAdapter: - 具有对应于特定月份项

 包com.dg.android.salarycalculator.adapter;

进口的java.util.ArrayList;
进口的java.util.Calendar;

进口com.dg.android.salarycalculator.R;
进口com.dg.android.salarycalculator.Salary;
进口com.dg.android.salarycalculator.views.ShiftListItem;

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

公共类MonthListAdapter扩展BaseExpandableListAdapter {

私人语境_context;
私人SparseArray< ArrayList的<薪酬和GT;> _shiftMonths;

公共MonthListAdapter(上下文的背景下,
        SparseArray< ArrayList的<薪酬和GT;> shiftMonths){
    超();

    _context =背景;
    _shiftMonths = shiftMonths;

}

公共对象getChild(INT groupPosition,诠释childPosition){
    返回(空== _shiftMonths)?空:_shiftMonths.get(
            _shiftMonths.keyAt(groupPosition))得到(childPosition)。

}

众长getChildId(INT groupPosition,诠释childPosition){
    返回childPosition;
}

公共查看getChildView(INT groupPosition,INT childPosition,
        布尔isLastChild,查看convertView,ViewGroup中父){


    ShiftListItem SLI;
    如果(空== convertView){
        SLI =(ShiftListItem)View.inflate(_context,
                R.layout.shift_list_item,NULL);
    } 其他 {
        SLI =(ShiftListItem)convertView;
    }
    sli.setSalary(_shiftMonths.get(_shiftMonths.keyAt(groupPosition))。得到(
            childPosition));
    返回SLI;
}





公众诠释getChildrenCount(INT groupPosition){
    如果(空== _shiftMonths
            ||空== _shiftMonths.get(_shiftMonths.keyAt(groupPosition)))
        返回0;
    返回_shiftMonths.get(_shiftMonths.keyAt(groupPosition))的大小()。
}

公共对象getGroup(INT groupPosition){
    返回(空== _shiftMonths)?空:_shiftMonths.get(_shiftMonths
            .keyAt(groupPosition));
}

公众诠释getGroupCount(){
    返回_shiftMonths.size();
}

众长getGroupId(INT groupPosition){
    返回groupPosition;
}

公共查看getGroupView(INT groupPosition,布尔isExpanded,
        查看convertView,ViewGroup中父){

    如果(convertView == NULL){
        LayoutInflater充气=(LayoutInflater)_context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.month_list_view_item,NULL);
    }

    TextView的标题=(TextView中)convertView.findViewById(R.id.monthTitle);
    title.setText(+ _shiftMonths.keyAt(groupPosition));

    返回convertView;

}

公共布尔hasStableIds(){
    返回true;
}

公共布尔isChildSelectable(INT groupPosition,诠释childPosition){
    返回true;
}

公共无效forceReload(){
    notifyDataSetChanged();
}

}
 

解决方案

我犯了一个新手的错误,并没有通过childID在 getChildView(...) YearListAdapter 类。这个问题已经解决了(希望正确)通过在 getChildView添加下一个命令(...)

monthLevelExpandable.setSelectedGroup(ARG1); ARG1 儿童地位中的参数传递)

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

}

解决方案

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).

这篇关于getGroupView(),在第二级expandableListView,总是具有相同的groupPosition所有二级项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆