0

我有以下自定义数组适配器 -

public class ColorAttributeArrayAdapter extends ArrayAdapter<ProductAttributeModel> {

    private List<ProductAttributeModel> titles;
    private Context context;
    private MarketApiCalls marketApiCalls;

    public ColorAttributeArrayAdapter(@NonNull Context context, List<ProductAttributeModel> titles) {
        super(context, R.layout.product_attribute_spinner_row_item, R.id.product_attribute_spinner_row_item_textview, titles);
//        super(context, 0,titles);
        this.titles = titles;
        this.context = context;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_PORTAL_PRODUCTION_URL)
//                .baseUrl(BASE_PORTAL_STAGE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        marketApiCalls = retrofit.create(MarketApiCalls.class);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View listItem = convertView;
        if (listItem == null) {
            listItem = LayoutInflater.from(context).inflate(R.layout.product_attribute_spinner_row_item, parent, false);
        }
//        String currentString = titles.get(position).getAttributeValues().get(position);
        List<String> attributeValues = titles.get(position).getAttributeValues();
        for (int i = 0; i < attributeValues.size(); i++) {
            String currentString = attributeValues.get(i);

            //Setting the image color
            ImageView imageView = listItem.findViewById(R.id.product_attribute_spinner_row_item_image_view);
            Map<String, String> htmlStandardColorMap = ColorUtil.getHtmlStandardColorMap();
            if (htmlStandardColorMap.containsKey(currentString)) {
                imageView.setBackgroundColor(Color.parseColor(htmlStandardColorMap.get(currentString)));
            } else {
                String colorURL = COLORS_API.concat(Uri.encode(currentString, "UTF-8"));
                Picasso.get().load(colorURL).resize(90,90).into(imageView);
            }

            TextView value = listItem.findViewById(R.id.product_attribute_spinner_row_item_textview);
            value.setText(currentString);

        }
        return listItem;
    }




    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return getView(position, convertView, parent);
    }
}

这是我创建适配器的方式 -

 for (Map.Entry<String, List<String>> entry : selectedProductAttributesMap.entrySet()) {
//Key and value for each iteration

  String key = entry.getKey();
  List<String> value = entry.getValue()

.
.
.
   ArrayList<ProductAttributeModel> productAttributeModels = new ArrayList<>();
   productAttributeModels.add(new ProductAttributeModel(key, value));
   ColorAttributeArrayAdapter adapter = new ColorAttributeArrayAdapter(this, productAttributeModels);
   spinner.setOnItemSelectedListener(this);
   spinner.setAdapter(adapter);
}

我的模特——

public class ProductAttributeModel {

    private String attributeName;

    private List<String> attributeValues;

    public ProductAttributeModel(String attributeName, List<String> attributeValues) {
        this.attributeName = attributeName;
        this.attributeValues = attributeValues;
    }

    public String getAttributeName() {
        return attributeName;
    }

    public void setAttributeName(String attributeName) {
        this.attributeName = attributeName;
    }

    public List<String> getAttributeValues() {
        return attributeValues;
    }

    public void setAttributeValues(List<String> attributeValues) {
        this.attributeValues = attributeValues;
    }

    @Override
    public String toString() {
        return "ProductAttributeModel{" +
                "attributeName='" + attributeName + '\'' +
                ", attributeValues=" + attributeValues +
                '}';
    }
}

问题是我在模型中放入了一个包含 4 个模型的列表,而微调器只显示其中一个。我错过了什么?

在此处输入图像描述

在此处输入图像描述

我试图迭代字符串数组的大小并将它们添加到每个行项目,但由于某种原因,它似乎覆盖了它的工作并且不添加而是将它们重写为另一个

编辑-这是我创建适配器时的完整方法-

@Override
    public void setProductPurchaseAttributes() {
        selectedProductAttributesMap = selectedProduct.getAttributesList();
        /*Starting index is the index in which we start to add the dynamic linear layouts that represents products attributes.
        This number should be incremented by 1 every time we do any changes to `activity_product_page.xml` file otherwise the dynamic views
        will be created in the wrong place.
         */
        int startingIndex = 7;
        if (!isProductAvailable) return;
        ArrayList<ProductAttributeModel> productAttributeModels = new ArrayList<>();
        for (Map.Entry<String, List<String>> entry : selectedProductAttributesMap.entrySet()) {

            //Key and value for each iteration

            String key = entry.getKey();
            List<String> value = entry.getValue();

            //creating the linear layout

            LinearLayout linearLayout = new LinearLayout(this);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);

            //creating the layout params

            LinearLayout.LayoutParams attributeLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            //setting margins
            /*These margins values are different than the values in the XML of the activity because this is a dynamically created view*/
            attributeLayoutParams.setMargins(48, 30, 48, 0);
            textParams.setMargins(0, 60, 0, 0);
            linearLayout.setLayoutParams(attributeLayoutParams);

            //creating the text view
            TextView textView = new TextView(this);

            textView.setText(key.concat(":"));
            textView.setLayoutParams(textParams);

            //creating the spinner
            Spinner spinner = new Spinner(this);
            spinner.setLayoutParams(spinnerParams);


            //attribute list adapter
            productAttributeModels.add(new ProductAttributeModel(key, value));
            ColorAttributeArrayAdapter adapter = new ColorAttributeArrayAdapter(this, productAttributeModels);
            spinner.setOnItemSelectedListener(this);
            spinner.setAdapter(adapter);

            //adding to the linear layout

            linearLayout.addView(textView);
            linearLayout.addView(spinner);

            //adding linear layout to root view

            productDetailsViewGroup.addView(linearLayout, startingIndex);
            startingIndex++;
        }
    }

如您所见,我正在动态创建微调器,这意味着如果我将微调器设置在循环之外,它将无法按预期工作。

4

1 回答 1

2

productAttributeModels首先在循环外声明您的列表

将您的productAttributeModels适配器设置在循环之外

示例代码

ArrayList<ProductAttributeModel> productAttributeModels = new ArrayList<>();

for (Map.Entry<String, List<String>> entry : selectedProductAttributesMap.entrySet()) {
//Key and value for each iteration

  String key = entry.getKey();
  List<String> value = entry.getValue()

  productAttributeModels.add(new ProductAttributeModel(key, value));
   
}
  ColorAttributeArrayAdapter adapter = new ColorAttributeArrayAdapter(this, productAttributeModels);
   spinner.setOnItemSelectedListener(this);
   spinner.setAdapter(adapter);
于 2019-12-12T09:34:20.023 回答