带有包含嵌套列表的列表的 ExpandableListViewAdapter

ExpandableListViewAdapter with List containing nested List

我正在尝试为这样的模型编写 ExpandableListViewAdapter

这是我的GetCheapestResponseType.class:

public class GetCheapestResponseType {

    List<Fares> fares;

}

这是我已经完成的,我不知道如何扩展 ExpandableListViewAdapter 的构造函数以仅传递 List<GetCheapestResponseType>:

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    private Context context;

    // group titles
    private List<GetCheapestResponseType> listDataGroup;

    // child data
    private List<Fares> listDataChild;

    public ExpandableListViewAdapter(Context context, List<GetCheapestResponseType> listDataGroup) {
        this.context = context;
        this.listDataGroup = listDataGroup;
        this.listDataChild = listDataGroup.get( /* what should be here */ ).getFares();
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this.listDataChild.get(groupPosition);
    }

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

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

        String departureCity = listDataChild.get(childPosition).getOutbound().getDepartureAirport().getName();
        String arrivalCity = listDataChild.get(childPosition).getOutbound().getArrivalAirport().getName();

        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.flight_data_item, null);
        }

        TextView departureCityTextView = convertView.findViewById(R.id.departureCityTextView);
        TextView arrivalCityTextView = convertView.findViewById(R.id.arrivalCityTextView);

        departureCityTextView.setText(departureCity);
        arrivalCityTextView.setText(arrivalCity);
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.listDataGroup.get(groupPosition).getFares().size();
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.flight_data_group, null);
        }

        TextView departureCityTextViewGroup = convertView
                .findViewById(R.id.departureCityTextViewGroup);
        departureCityTextViewGroup.setTypeface(null, Typeface.BOLD);
        departureCityTextViewGroup.setText(headerTitle);

        return convertView;
    }
}

无法完全重现您的数据列表。下面的适配器显示了如何处理类似的列表。希望对您有所帮助!

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

public class GetCheapestResponseType{
    // String departureCity;
    List<Fares> fares;

    // public String getDepartureCity() {
    //     return departureCity;
    // }
}

public class Fares{
    String departureAirport, arrivalAirport;

    public String getDepartureAirport() {
        return departureAirport;
    }

    public String getArrivalAirport() {
        return arrivalAirport;
    }
}

private Context context;

private List<GetCheapestResponseType> listData;

public ExpandableListViewAdapter(Context context, List<GetCheapestResponseType> listData) {
    this.context = context;
    this.listData = listData;
}

@Override
public Fares getChild(int groupPosition, int childPosition) {
    return this.listData.get(groupPosition).fares.get(childPosition);
}

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

    String departureCity = getChild(groupPosition, childPosition).getDepartureAirport();
    String arrivalCity = getChild(groupPosition, childPosition).getArrivalAirport();

    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.flight_data_item, null);
    }
    TextView departureCityTextView = convertView.findViewById(R.id.departureCityTextView);
    TextView arrivalCityTextView = convertView.findViewById(R.id.arrivalCityTextView);
    departureCityTextView.setText(departureCity);
    arrivalCityTextView.setText(arrivalCity);

    return convertView;
}

@Override
public boolean isChildSelectable(int i, int i1) {
    return false;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this.listData.get(groupPosition).fares.size();
}

@Override
public GetCheapestResponseType getGroup(int groupPosition) {
    return this.listData.get(groupPosition);
}

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

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    // String headerTitle = getGroup(groupPosition).getDepartureCity();
    String headerTitle = getGroup(groupPosition).fares.get(0).getDepartureAirport();

    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.flight_data_group, null);
    }
    TextView departureCityTextViewGroup = convertView.findViewById(R.id.departureCityTextViewGroup);
    departureCityTextViewGroup.setTypeface(null, Typeface.BOLD);
    departureCityTextViewGroup.setText(headerTitle);

    return convertView;
}

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

@Override
public boolean hasStableIds() {
    return false;
}
}