I have a problem with showing first item selected at spinner dropdown menu. When spinner is first time initialized, row is filled with "nothing selected view" and when something is selected from the dropdown menu, spinner view is changed with selected value from dropdown. That works in every case except in the case when I select first item right after the initialization. What I'm trying to say is that value of spinner row writes value of selected item in dropdown in every case except 0 item. Zero item shows in spinner only if item 0> is selected before. If 0 is selected right after the initialization of spinner, it wont show.
That leads me to conclusion that adapter works in strange way. When spinner is initialized it is filled with default. Afterwards, if selected item is above default value it will change that default, but if default isn't change, the state stays the same? In other words, Spinner will only change view in case of different selected value from current one? Other thing that bothers me is that in the getView method, I get the right value, right position, but the view won't change anyway. Like something overrides override method and won't let view to change if value is 0.
Spinner in fragment
spinnerHairColor.setAdapter(new CustomSpinnerAdapter(R.string.hair_color,
getContext(), R.layout.spinner_dropdown, values.getHair_color()));
spinnerHairColor.setFocusableInTouchMode(true);
spinnerHairColor.setOnFocusChangeListener(spinnerFocusListener);
Adapter
public class CustomSpinnerAdapter extends ArrayAdapter<Values.ValuesProperty> implements SpinnerAdapter {
private Context context;
private List<Values.ValuesProperty> valuesProperty;
protected LayoutInflater layoutInflater;
private int unselectedText;
private boolean init = false;
public CustomSpinnerAdapter(int unselectedText, Context context, int nothingSelectedLayout, List<Values.ValuesProperty> valuesProperty) {
super(context, nothingSelectedLayout, valuesProperty);
this.unselectedText = unselectedText;
this.valuesProperty = valuesProperty;
layoutInflater = LayoutInflater.from(context);
this.context=context;
init = true;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = layoutInflater.inflate(R.layout.spinner_dropdown, parent, false);
TextView tv = (TextView) row.findViewById(R.id.spinnerNothingText);
if (position == 0 && init) {
return getNothingSelectedView(parent);
}
Values.ValuesProperty v = getItem(position);
tv.setText(getContext().getText(unselectedText) + ": " + v.getName());
return row;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
Values.ValuesProperty v = getItem(position);
View row = layoutInflater.inflate(R.layout.item_spinner, parent, false);
TextView tv = (TextView) row.findViewById(R.id.spinnerText);
tv.setText(v.getName());
return row;
}
protected View getNothingSelectedView(ViewGroup parent)
{
View backView = layoutInflater.inflate(R.layout.spinner_dropdown, parent, false);
TextView tv = (TextView) backView.findViewById(R.id.spinnerNothingText);
tv.setText(getContext().getText(unselectedText));
// to make sure if 0 is selected isnt inital 0
init = false;
return backView;
}
}