ExpandableListViewAdapter with List containing nested List

て烟熏妆下的殇ゞ 提交于 2021-02-11 06:48:28

问题


I am trying to write ExpandableListViewAdapter for model like this:

This is my GetCheapestResponseType.class:

public class GetCheapestResponseType {

    List<Fares> fares;

}

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

回答1:


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


来源:https://stackoverflow.com/questions/61191677/expandablelistviewadapter-with-list-containing-nested-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!