具有包含嵌套列表的列表的ExpandableListViewAdapter [英] ExpandableListViewAdapter with List containing nested List

查看:51
本文介绍了具有包含嵌套列表的列表的ExpandableListViewAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write ExpandableListViewAdapter for model like this:

这是我的 GetCheapestResponseType.class :

public class GetCheapestResponseType {

    List<Fares> fares;

}

这是我已经做过的,我不知道如何扩展 ExpandableListViewAdapter 的构造方法以仅传递 List< GetCheapestResponseType> :

This is what I have already done, I don't know how to expand Constructor of ExpandableListViewAdapter to pass only 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;
    }
}

推荐答案

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

Cannot completely reproduce your datalist. Below adapter shows how to handle a similar list. Hope that helps!

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

这篇关于具有包含嵌套列表的列表的ExpandableListViewAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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