2017-02-23 8 views
0

表示された値の配列を持つNumberPickerを作成したいとします。 NumberPickerが表示されても値は変更されませんが、最小値と最大値はユーザーの操作に応じて動的に変化します。ここNumberPickerのsetMinValueが正しい最小値を選択しない

コードである:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <NumberPicker 
     android:id="@+id/picker" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true"/> 
</RelativeLayout> 
主な活動で

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final String[] values = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; 

     NumberPicker picker = (NumberPicker) findViewById(R.id.picker); 
     picker.setDisplayedValues(values); 
     picker.setMaxValue(7); 
     picker.setMinValue(3); 

     picker.setWrapSelectorWheel(false); 
    } 
} 

Iが最小に設定せずに最大に設定すると、私は( "0" と "7" の間に表示された値で終わります含まれています)。 minValueを0に設定すると、私は同じ動作をします。 しかし、minValueを3に設定すると、 "0"と "4"の間に表示された値で終了します。それが「3」と「7」の間にあるとき。

なぜか分かりません。間違ったことをやったことがありますか、コンポーネントに問題がありますか?

答えて

2

氏は述べていますコメントあり表示された値を更新します。簡潔にするため

//do this once in onCreate and save the values list somewhere where you have access 
final List<String> values = new ArrayList<>(); 
for (int i = 0; i < 10; ++i) { 
    values.add(String.valueOf(i)); 
} 
NumberPicker picker = (NumberPicker) findViewById(R.id.picker); 
picker.setWrapSelectorWheel(false); 


//do this every time you want to change the displayed values and set minValue and maxValue accordingly 
//also make sure that you stay within the bounds of value 
int minValue = 3; 
int maxValue = 7; 

final List<String> subValues = values.subList(minValue, maxValue + 1); 
String[] subValuesArray = new String[subValues.size()]; 
subValuesArray = subValues.toArray(subValuesArray); 
picker.setDisplayedValues(subValuesArray); 
picker.setMinValue(0); 
picker.setMaxValue(subValues.size() - 1); 
+0

する必要がありますこれを詳しく教えていただけますか? OPの問題を解決しないのはなぜですか?また、最小値/最大値が変わるたびに表示値を変更するように提案していませんでしたか(私はそれを誤解している可能性があります)? – Bmuig

+0

申し訳ありませんが、私はあなたの提案を誤解しました。それは期待どおりに動作します。そのために残念! –

1

the documentationから説明できます。

minとmaxを設定すると、NumberPickerで入力できるint値を定義します。したがって、setValue(int)は最小値から最大値までの値しか受け入れません。

displayValues()はラベルオーバーライドです。そしてそれは次の方法で使用されます - NumberPickerはインデックスvalue - minでラベルを受け取ります。だから、

* <strong>Note:</strong> The length of the displayed values array 
* set via {@link #setDisplayedValues(String[])} must be equal to the 
* range of selectable numbers which is equal to 
* {@link #getMaxValue()} - {@link #getMinValue()} + 1. 

は、この問題を回避するために、それはあなたがしたいときだけ値のリストを設定するのが最適です:あなたはsetMinValueまたはsetMaxValueの実装を見れば

+0

だから、それは私が表示された値の配列に私が最小値と最大値を変更するたびに変更しなければならないことを意味しますか? – Eselfar

+0

残念ながら、はいあなたは –

0

@Bmuig答え、ちょうどヘルパー関数へ:

private void configurePicker(NumberPicker picker, ArrayList<String> values, int minValue, int maxValue, int currentValue) { 
    picker.setDisplayedValues(values) //to avoid out of bounds 
    picker.setMinValue(minValue); 
    picker.setMaxValue(maxValue); 
    picker.setValue(currentValue); 
    picker.setDisplayedValues((String[]) values.subList(minValue, maxValue + 1).toArray()); 
} 
関連する問題