Java - 在自定义适配器中创建对两个模型的引用
Java - Create reference to two models in a custom adapter
我有两个名为 Buyer
和 Car
的模型,以及一个名为 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
的引用,以便我可以在同一个 ListView
中获取和显示来自模型 Buyer
和模型 Car
的信息]?
您可以使用 Buyer
和 Car
作为属性创建一个新的 class:
class Transaction{
public Buyer buyer;
public Car car;
}
并在您的 ArrayList
(ArrayList<Transaction>
) 中使用它。
一种方法是创建一个包含汽车和买家数据的模型。
这样你就可以从同一个数组列表中访问汽车和买家。
另一个是将两个数组列表(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());
不是创建 Buyer
的 ArrayList,而是像这样创建 Pair<Buyer,Car>
的 ArrayList:
ArrayList<Pair<Buyer,Car>> myBuyAndCarPair;
当您希望获得 Buyer
或 Car
对象时,可以这样调用它们:
Buyer myBuyer = myBuyAndCarPair.get(i).first;
Car myCar = myBuyAndCarPair.get(i).second;
创建新对:
new Pair<>(mBuyerObject,mCarObject);
我有两个名为 Buyer
和 Car
的模型,以及一个名为 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
的引用,以便我可以在同一个 ListView
中获取和显示来自模型 Buyer
和模型 Car
的信息]?
您可以使用 Buyer
和 Car
作为属性创建一个新的 class:
class Transaction{
public Buyer buyer;
public Car car;
}
并在您的 ArrayList
(ArrayList<Transaction>
) 中使用它。
一种方法是创建一个包含汽车和买家数据的模型。 这样你就可以从同一个数组列表中访问汽车和买家。
另一个是将两个数组列表(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());
不是创建 Buyer
的 ArrayList,而是像这样创建 Pair<Buyer,Car>
的 ArrayList:
ArrayList<Pair<Buyer,Car>> myBuyAndCarPair;
当您希望获得 Buyer
或 Car
对象时,可以这样调用它们:
Buyer myBuyer = myBuyAndCarPair.get(i).first;
Car myCar = myBuyAndCarPair.get(i).second;
创建新对:
new Pair<>(mBuyerObject,mCarObject);