自定义可扩展BaseExpandableListAdapter getGroupView或getChildView永远不会被调用

时间:2015-03-16 16:04:37

标签: android expandablelistadapter

我有一个Viewpager有2个页面,在第1页上我填写表格并滑动到第2页,其中包含expandableList,我使用的是自定义适配器,如下所示。

我已将可展开的listview和另一个按钮包含在布局中。

当我滑动到这个新页面时,我看到空白布局和按钮,可扩展列表从不显示。

我检查过并发现getGroupCount返回的大小相当于arrayList数据的大小。所以这不是数据空的问题。

在进一步分析中,我发现getGroupView以及getChildView永远不会被调用,我在这些方法中添加了一些日志语句来调试,它们永远不会打印出来。但是,正在调用构造函数,getGroupCoun返回正确的值也是如此。我的实施是否正确?

public class FareStageExpandableListAdapter extends BaseExpandableListAdapter

{
private Context context;
private LayoutInflater inflater;
private ArrayList<TripTimings> data;
private SimpleDateFormat formatter;
private EditText fareField;
private TextView headerText;
private TextView childText;

public FareStageExpandableListAdapter(Context context,
        ArrayList<TripTimings> data)
{
    Log.d("FareStageExADapter", "Constructor called");
    this.context = context;
    this.inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.data = data;
    this.formatter = new SimpleDateFormat("HH:mm");

}

@Override
public int getGroupCount()
{
    return data.size();
}

@Override
public int getChildrenCount(int groupPosition)
{
    return data.size() - 1 - groupPosition;
}

@Override
public Object getGroup(int groupPosition)
{
    return data.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition)
{
    return data.get(groupPosition + childPosition);
}

@Override
public long getGroupId(int groupPosition)
{
    return data.get(groupPosition).hashCode();
}

@Override
public long getChildId(int groupPosition, int childPosition)
{
    return data.get(groupPosition + childPosition).hashCode() + 5;
}

@Override
public boolean hasStableIds()
{
    // TODO Auto-generated method stub
    return true;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parentView)
{
    TripTimings parent = data.get(groupPosition);
    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.fareadd_expanded_parent,
                parentView, false);
    }
    headerText = ((TextView) convertView.findViewById(R.id.originText));

    Log.d("Setting header text ", parent.getStop().getStop_name());
    headerText.setText(parent.getStop().getStop_name());
    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup childView)
{
    final TripTimings child = data.get(groupPosition + childPosition);
    if (convertView == null)
    {
        convertView = inflater.inflate(R.layout.fareadd_expanded_child,
                childView, false);
    }
    childText = ((TextView) convertView.findViewById(R.id.destinationText));

    Log.d("Setting child text ", child.getStop().getStop_name());
    childText.setText(child.getStop().getStop_name());

    return convertView;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
    // TODO Auto-generated method stub
    return false;
}

}

1 个答案:

答案 0 :(得分:1)

当你睡眠不足时会发生这种情况。除了从未将适配器添加到expandablelistview

之外,代码没有任何问题