2017-04-26 46 views
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

回答

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; 
    } 
} 
相關問題