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);
        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);
    }
}
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 +
                '}';
    }
}

我正在从以下功能启动我的适配器 -

 @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<>();
        Spinner spinner = new Spinner(this);
        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.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++;
        }
    }

出于某种原因,我只得到一个结果而不是多个结果。

将适配器实例化到 for 循环的末尾将无济于事,已经尝试过了。

在进行调试时,我看到适配器的对象在列表中包含 4 个项目,但我只显示最后一个。好像我正在编写对象而不是添加它。我错过了什么?

4

1 回答 1

0

将这些行完全移出 for 循环

   ColorAttributeArrayAdapter adapter = new ColorAttributeArrayAdapter(this, productAttributeModels);
   spinner.setOnItemSelectedListener(this);
   spinner.setAdapter(adapter);

您当前正在使用最新的属性模型在 for 循环的每次运行中创建一个新适配器并将其附加到微调器,因此微调器在到达末尾时只有最后一个具有最后一个属性模型的适配器。在 for 循环之外创建的适配器将具有您添加到productAttributeModels列表中的所有属性。

于 2019-12-12T16:44:43.520 回答