Reputation: 1298
I have the following custom 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);
}
}
and here is how I create the adapter -
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);
}
my model -
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 +
'}';
}
}
The issue is that I am putting inside the model a list containing 4 models and the spinner is showing only one of them. What am I missing?
I tried to iterate though the size of the array of strings and add them to each row item but for some reason it seems to override it's work and does not add but re-writes them one over the other
edit - here is my full method when creating the adapter -
@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++;
}
}
As you can see, I am creating the spinner dynamically which means that if I set the spinner outside of the loop it won't work as expected.
Upvotes: 0
Views: 164
Reputation: 69699
First declare your
productAttributeModels
list outside loopSet your
productAttributeModels
adapter outside loop
SAMPLE CODE
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);
Upvotes: 2