Selected item at position 0 in android Spinner
Asked Answered
C

1

2

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;
    }

}
Coverture answered 26/4, 2017 at 13:4 Comment(2)
I thing there might be problem with your color array. can you pls Post your values.getHair_color(); response.Carpus
As I wrote, I don't have trouble with values, but with showing them in the case of first click if the first value is clicked. In the getView at the point before tv.setText I always get right value and right position. Just for some reason it wont show if it's 0 position and some higher position isn't selected before.Coverture
C
0

I managed to come up with a solution. This is the adapter that works for the spinner that may have default value if nothing is selected

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) {
        init = false;
        tv.setText(getContext().getText(unselectedText));
        return row;
    }

    Values.ValuesProperty v = getItem(position);
    if (position == 0 && parent.hasFocus())
        notifyDataSetChanged();

    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 rowDrop = layoutInflater.inflate(R.layout.item_spinner, parent, false);
    TextView tvDrop = (TextView) rowDrop.findViewById(R.id.spinnerText);
    tvDrop.setText(v.getName());
    return rowDrop;
    }
}
Coverture answered 27/4, 2017 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.