如何将微调器设置为 Recyclerview 适配器
How to Setup Spinner Into Recyclerview Adapter
我知道这个问题已经被问过好几次了,但我不太明白。我有一个适配器,对于每个项目,我都需要设置一个微调器。
对于上下文,我正在开发一种产品销售应用程序。因此适配器中装满了促销中的产品,微调器必须具有与该促销中的产品一样多的选项,在我的代码中是数量值。
这是我的适配器代码
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);
}
}
}
任何关于如何在 cardview 中制作 sppiner 的帮助或建议都将非常有用,谢谢!
试试这个,在 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;
}
我知道这个问题已经被问过好几次了,但我不太明白。我有一个适配器,对于每个项目,我都需要设置一个微调器。 对于上下文,我正在开发一种产品销售应用程序。因此适配器中装满了促销中的产品,微调器必须具有与该促销中的产品一样多的选项,在我的代码中是数量值。
这是我的适配器代码
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);
}
}
}
任何关于如何在 cardview 中制作 sppiner 的帮助或建议都将非常有用,谢谢!
试试这个,在 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;
}