我已经做到了
我有一个列表,在 3 种不同类型的对象列表中(如旅游/酒店/签证)
所以三种不同类型的对象列表每个列表都包含不同类型的对象,但是我使用了一个扭曲来识别我有三种类型的对象列表,所以我已经将 Map> 传递给适配器,检查下面例子
BookingDetails details = gson.fromJson(result, BookingDetails.class);
Map<String, List<Object>> serviceListMap = new HashMap<>();
if (details.getHotelList() != null) {
List<Object> list = new ArrayList<>();
list.addAll(details.getHotelList());
serviceListMap.put("Hotel", list);
}
if (details.getTourList() != null) {
List<Object> list = new ArrayList<>();
list.addAll(details.getTourList());
serviceListMap.put("Tour", list);
}
if (details.getVisaList() != null) {
List<Object> list = new ArrayList<>();
list.addAll(details.getVisaList());
serviceListMap.put("Visa", list);
}
if (serviceListMap.size() > 0){
HomeListAdapter adapter = new HomeListAdapter(HomeActivity.this, serviceListMap);
listView.setAdapter(adapter);
}
并在 HomeListAdapter
public class HomeListAdapter extends BaseAdapter {
private List<String> mDataList;
private Map<String, List<Object>> detailMap;
private Context mContext;
public HomeListAdapter(Context context, Map<String, List<Object>> dataList) {
mContext = context;
mDataList = new ArrayList<>(dataList.keySet());
detailMap = dataList;
}
@Override
public int getCount() {
return mDataList.size();
}
@Override
public String getItem(int position) {
return mDataList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String key = mDataList.get(position);
final HolderVoucher holderVoucher;
if (convertView == null) {
convertView = View.inflate(mContext, R.layout.item_row, null);
holderVoucher = new HolderVoucher();
convertView.setTag(holderVoucher);
holderVoucher.title = convertView.findViewById(R.id.title);
holderVoucher.checkin = convertView.findViewById(R.id.checkin);
holderVoucher.checkout = convertView.findViewById(R.id.checkout);
} else {
holderVoucher = (HolderVoucher) convertView.getTag();
}
StringBuilder desc = null;
holderVoucher.title.setText(key);
List<Object> services = detailMap.get(key);
desc = new StringBuilder();
if (services.size() > 0) {
for (int i = 0; i < services.size(); i++) {
desc.append("\n");
if (services.size() > 1) {
desc.append((i + 1) + ". ");
}
if (key.equalsIgnoreCase("Visa")) {
VisaDetails detail = (VisaDetails) services.get(i);
desc.append(detail.getVisaName());
desc.append("\nTravel Date : " + detail.getTravelDate()));
} else if (key.equalsIgnoreCase("Hotel")) {
HotelDetails detail = (HotelDetails) services.get(i);
desc.append(detail.getHotelName() + "\n" + detail.getRoomType());
desc.append("\nCheck in : " + detail.getCheckInDate()) +
" Check out : " + detail.getCheckOutDate()));
} else if (key.equalsIgnoreCase("Tour")) {
TourDetails detail = (TourDetails) services.get(i);
desc.append("Tour - " + detail.getTourName());
desc.append("\nTravel Date : " +detail.getTravelDate()));
}
holderVoucher.checkin.setText(desc.toString());
return convertView;
}
private class HolderVoucher {
private TextView title, checkin, checkout;
}
}
如果您对上述详细信息有任何疑问,请告诉我。