I know this has been asked several times but I do not quite get it. I have an adapter and for every item, I need to set up a spinner. For context, I am working on a sort of product sales application. So the adapter is filled with products from the sale and the spinner has to have as many options as the product in that sale, which in my code is the quantity value.
Here is my adapter code
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);
}
}
}
Any help or suggestions on how to approach the making of the sppiner inside the cardview would be great, thanks!
try this, inside the 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);
and then implement the qArray function
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;
}