gpt4 book ai didi

Java - 在自定义适配器中创建对两个模型的引用

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:49 24 4
gpt4 key购买 nike

我有两个名为 BuyerCar 的模型,以及一个名为 custom_row 的自定义布局,用于显示 ListView.

public class CustomAdapter extends BaseAdapter {
Context c;
ArrayList<Buyer> buyers;

public CustomAdapter(Context c, ArrayList<Buyer> buyers) {
this.c = c;
this.buyers = buyers;
}

@Override
public int getCount() {
return buyers.size();
}

@Override
public Object getItem(int position) {
return buyers.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(c).inflate(R.layout.custom_row, parent, false);
}

TextView tvBuyerName = (TextView) convertView.findViewById(R.id.tvBuyerName);
TextView tvCarModel = (TextView) convertView.findViewById(R.id.tvCarModel);

final Buyer b = (Buyer) this.getItem(position);

tvBuyerName.setText(b.getBuyerName());

return convertView;
}
}

到目前为止我只完成了上面的代码,而且我只能显示买家的名字。如何在 ArrayList 中创建另一个对 Car 模型的引用,以便我可以获取并显示来自模型 Buyer 和模型 的信息Car 在同一个 ListView 中?

最佳答案

一种方法是创建一个包含汽车和买家数据的模型。这样你就可以从同一个数组列表中访问汽车和买家。

另一个是将两个数组列表(carList 和 buyerList)传递给适配器的构造函数。

ArrayList<Buyer> buyers;
ArrayList<Car> cars;

public CustomAdapter(Context c, ArrayList<Buyer> buyers, ArrayList<Car> cars) {
this.c = c;
this.buyers = buyers;
this.cars= cars;
}

然后

final Buyer b = (Buyer) buyers.getItem(position);
tvBuyerName.setText(b.getBuyerName());

final Car c = (Car) cars.getItem(position);
tvCar.setText(c.getCarName());

关于Java - 在自定义适配器中创建对两个模型的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38941249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com