android Spinner中位置0的所选项目

时间:2017-04-26 13:04:23

标签: android spinner android-spinner android-adapter

显示在微调器下拉菜单中选择第一个项目时出现问题。当第一次初始化微调器时,行将填充“无选择视图”,当从下拉菜单中选择某些内容时,微调器视图将从下拉列表中选择值进行更改。这在每种情况下都有效,除非我在初始化后立即选择第一项。我想说的是,除了0项之外,每个情况下,微调器行的值都会在下拉列表中写入所选项目的值。仅当项目0>时,零项目才会在微调器中显示之前被选中。如果在微调器初始化后立即选择0,则不会显示。

这使我得出结论,适配器以奇怪的方式工作。 当微调器初始化时,它将被默认填充。之后,如果所选项目高于默认值,它将更改该默认值,但如果默认值未更改,则状态保持不变?换句话说,Spinner只会在当前值不同的情况下更改视图?困扰我的另一件事是,在getView方法中,我得到正确的值,正确的位置,但视图不会改变。就像覆盖覆盖方法一样,如果值为0,则不允许视图改变。

片段中的微调器

spinnerHairColor.setAdapter(new CustomSpinnerAdapter(R.string.hair_color, 
    getContext(), R.layout.spinner_dropdown, values.getHair_color()));
spinnerHairColor.setFocusableInTouchMode(true);
spinnerHairColor.setOnFocusChangeListener(spinnerFocusListener);

适配器

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

}

1 个答案:

答案 0 :(得分:0)

我设法找到了解决方案。这是适用于微调器的适配器,如果未选择任何内容,则可能具有默认值

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