我有以下自定义arrayadapter-
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++; } }
如您所见,我正在动态创建微调器,这意味着如果将微调器设置在循环之外,它将无法按预期工作。
我有以下自定义arrayadapter-公共类ColorAttributeArrayAdapter扩展了ArrayAdapter
首先在循环外声明您的
productAttributeModels
列表在循环外设置
productAttributeModels
适配器