我知道这个问题已经被问过好几次了,但我不太明白。我有一个适配器,每个项目都需要设置一个微调器。就上下文而言,我正在开发一种产品销售应用程序。因此,适配器中充满了来自销售的产品,微调器必须有与销售中的产品一样多的选项,在我的代码中,这就是数量值。
这是我的适配器代码
public class MoneyDevolutionAdapter extends RecyclerView.Adapter < MoneyDevolutionAdapter.SaleDetailsViewHolder > implements View.OnClickListener {
private List < SaleDetail > saleDetailList;
private View.OnClickListener listener;
private List < Integer > quantityList;
public MoneyDevolutionAdapter(List < SaleDetail > saleDetailList) {
this.saleDetailList = saleDetailList;
}
@NonNull
@Override
public MoneyDevolutionAdapter.SaleDetailsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.adapter_money_devolution, viewGroup, false);
v.setOnClickListener(this);
return new MoneyDevolutionAdapter.SaleDetailsViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MoneyDevolutionAdapter.SaleDetailsViewHolder saleDetailsViewHolder, int i) {
Product product = new Product().findProductById(saleDetailList.get(i).idProduct);
if (product != null) {
saleDetailsViewHolder.txtNameSaleDetailsAdapter.setVisibility(View.VISIBLE);
saleDetailsViewHolder.txtNameSaleDetailsAdapter.setText("" + product.nameProduct);
}
saleDetailsViewHolder.txtIdSaleDetailsAdapter.setText("" + saleDetailList.get(i).idProduct);
saleDetailsViewHolder.txtQuantitySaleDetailsAdapter.setText("" + saleDetailList.get(i).quantity);
saleDetailsViewHolder.txtSVSaleDetailsAdapter.setText("" + Tools.setDecimalFormat().format(saleDetailList.get(i).unitValue));
saleDetailsViewHolder.txtValuetotSaleDetailsAdapter.setText("" + Tools.setDecimalFormat().format(saleDetailList.get(i).totValue));
}
@Override
public int getItemCount() {
return saleDetailList.size();
}
public void setOnClickListener(View.OnClickListener listener) {
this.listener = listener;
}
@Override
public void onClick(View v) {
if (listener != null) {
listener.onClick(v);
}
}
static class SaleDetailsViewHolder extends RecyclerView.ViewHolder {
TextView txtNameSaleDetailsAdapter;
TextView txtIdSaleDetailsAdapter;
TextView txtQuantitySaleDetailsAdapter;
TextView txtSVSaleDetailsAdapter;
TextView txtValuetotSaleDetailsAdapter;
Spinner spnQuantityProduct;
SaleDetailsViewHolder(View itemView) {
super(itemView);
txtNameSaleDetailsAdapter = itemView.findViewById(R.id.txtNameSaleDetailsAdapter);
txtIdSaleDetailsAdapter = itemView.findViewById(R.id.txtIdSaleDetailsAdapter);
txtQuantitySaleDetailsAdapter = itemView.findViewById(R.id.txtQuantitySaleDetailsAdapter);
txtSVSaleDetailsAdapter = itemView.findViewById(R.id.txtSVSaleDetailsAdapter);
txtValuetotSaleDetailsAdapter = itemView.findViewById(R.id.txtValuetotSaleDetailsAdapter);
spnQuantityProduct = itemView.findViewById(R.id.spnQuantityProducttoDevolution);
}
}
}
关于如何在卡片视图中制作间谍软件的任何帮助或建议都将非常好,谢谢!
在onBindViewHolder:
List<String> quantityArray = qArray(saleDetailList.get(i).quantity);
ArrayAdapter<String> dataAdapter =
new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, quantityArray);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
saleDetailsViewHolder.spnQuantityProduct.setAdapter(dataAdapter);
然后实现qArray功能
private static List<String> qArray(int quantity){
List<String> stringsArray = new ArrayList<>();
for (int i=0;i<quantity;i++){
stringsArray.add(String.valueOf(i+1));
}
return stringsArray;
}