android Spinner中位置0处的选定项目 [英] Selected item at position 0 in android Spinner

查看:62
本文介绍了android Spinner中位置0处的选定项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显示微调器下拉菜单中选择的第一项时出现问题.首次初始化微调器时,该行将填充没有选定的视图",并且从下拉菜单中选择某些内容时,微调器视图将使用下拉菜单中的选定值进行更改.这在每种情况下都有效,除了在初始化后立即选择第一个项目的情况下.我要说的是,在每种情况下,微调器行的值都会在下拉列表中写入所选项目的值,但0项除外.仅在之前选择项目0>时,微调器中才会显示零项目.如果微调器初始化后立即选择0,则不会显示.

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.

这使我得出结论,适配器以奇怪的方式工作. 旋转器初始化后,将使用默认值进行填充.之后,如果所选项目高于默认值,它将更改该默认值,但是如果默认值未更改,则状态保持不变?换句话说,微调器只会在所选值与当前值不同的情况下更改视图吗?令我困扰的另一件事是,在getView方法中,我获得了正确的值,正确的位置,但是视图始终不会改变.就像某些东西会覆盖重写方法,并且如果value为0,将不允许视图更改.

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.

片段中的旋转器

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

}

推荐答案

我设法提出了一个解决方案.这是适用于微调器的适配器,如果未选择任何选项,则可能具有默认值

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

这篇关于android Spinner中位置0处的选定项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆