我的自定义模型具有以下自定义阵列适配器-
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个项目,但仅显示最后一个。好像我在写对象而不是添加它。 我想念什么?
答案 0 :(得分:0)
将这些行完全移到for循环之外
ColorAttributeArrayAdapter adapter = new ColorAttributeArrayAdapter(this, productAttributeModels);
spinner.setOnItemSelectedListener(this);
spinner.setAdapter(adapter);
您当前正在使用最新的属性模型在for循环的每次运行中创建一个新适配器,并将其附加到微调器,因此微调器到达末尾时仅具有最后一个属性模型的最后一个适配器。在for循环外创建的适配器将具有您添加到productAttributeModels
列表中的所有属性。