2017-04-26 4 views
0

スピナードロップダウンメニューで最初に選択されたアイテムを表示する際に問題があります。スピナーが最初に初期化されると、行は「何も選択されていないビュー」で満たされ、ドロップダウンメニューから何かが選択されると、ドロップダウンから選択された値でスピナービューが変更されます。これは、初期化の直後に最初の項目を選択する場合を除いて、すべての場合に機能します。私が言いたいのは、スピナー行の値が0アイテムを除くすべてのケースでドロップダウンで選択したアイテムの値を書き込むということです。前のアイテム0>が選択されている場合にのみ、ゼロアイテムがスピナーに表示されます。スピナーの初期化直後に0を選択した場合は、表示されません。アンドロイドスピナーのポジション0で選択されたアイテム

これは、アダプターが変な形で動作するという結論に至ります。 スピナーが初期化されると、デフォルトで塗りつぶされます。その後、選択した項目がデフォルト値を超えていれば、そのデフォルト値が変更されますが、デフォルト値が変更されていなければ、状態は変わりません。言い換えれば、Spinnerは現在のものとは異なる選択された値の場合にのみビューを変更するでしょうか?私を悩ます他の事は、getViewメソッドで正しい値、正しい位置が得られるが、ビューはとにかく変更されないということです。何かのようにオーバーライドメソッドをオーバーライドし、値が、私は解決策を考え出すために管理フラグメント

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

カラー配列に問題があるかもしれません。あなたplsあなたの値を投稿できますか?getHair_color();応答。 –

+0

私が書いたように、私は値に問題はありませんが、最初の値がクリックされた場合、最初のクリックの場合にそれらを表示します。 tv.setTextの前の時点のgetViewでは、常に正しい値と正しい位置を取得します。何らかの理由でちょうどそれが0の位置であり、いくつかの上位の位置が選択されていない場合は表示されません。 – tompadre

答えて

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; 
    } 
} 
関連する問題