在Android Spinner中的位置0处的选定项目

问题描述:

我在显示在Spinner下拉菜单中选择的第一个项目时遇到问题。当第一次初始化微调控制器时,行会填充“没有选定的视图”,并且当从下拉菜单中选择某些内容时,微调控制器视图将从下拉菜单中选择的值更改。除了在初始化后立即选择第一个项目的情况以外,每种情况都适用。我想说的是,除了0项外,微调器行的值在下拉列表中选定项目的值。只有在选择了项目0>之前,零项目才会显示在微调框中。如果在微调器初始化后立即选择0,则不会显示。在Android Spinner中的位置0处的选定项目

这使我得出结论:适配器以奇怪的方式工作。 微调框初始化时,它被填充为默认值。之后,如果选定的项目高于默认值,则会更改该默认值,但如果默认值未更改,则状态保持不变?换句话说,微调只会在当前选择的值不同的情况下更改视图?其他困扰我的事情是,在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; 
    } 

} 
+0

我可能有问题,你的颜色数组。你可以请你发布你的values.getHair_color();响应。 –

+0

正如我写的,我没有价值的问题,但显示他们在第一次点击的情况下,如果第一个值被点击。在tv.setText之前的getView中,我始终得到正确的值和正确的位置。只是出于某种原因,它不会显示它是否为0位置,并且之前没有选择某个更高的位置。 – tompadre

我设法拿出一个解决方案。这是适用于微调,可能有默认值,如果没有选择

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