Search code examples
javaandroidnumberpicker

How to handle NumberPicker Value change?


I have a NumberPicker that contains titles of games. The thing is I want that in each time a person scroll the NumberPicker and stops at one of the titles, I will be able to immediately handle this action and fill 2 EditText with information I want.

On button press there is void onClick(View view) that handles each time a Button is pressed on a view, How do I do a similar thing with NumberPicker?

This is my code:

NumberPicker picker;
SharedPreferences sp;
String JsonDataGL;
String[] arrayGL;
ProgressDialog p;
GamesLibrary[] gamesArray;
String Count = "0";
EditText gameTitleET, gameContentET;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_post);
    sp=getSharedPreferences("JsonData", 0);
    JsonDataGL = sp.getString("JsonGL", null);

    gameTitleET = findViewById(R.id.GameTitleET); //The 2 EditTexts I want to fill with text
    gameContentET = findViewById(R.id.GameTitleET);

    JSONObject json_data = null;
    try {
        json_data = new JSONObject(JsonDataGL);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        Count = json_data.getString("count");
        Log.d("Nugi32", Count);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    gamesArray = new GamesLibrary[Integer.parseInt(Count)];

    picker = (NumberPicker) findViewById(R.id.numberPicker2);
    picker.setMinValue(0);
    picker.setMaxValue(Integer.parseInt(Count)-1);
    arrayGL = new String[Integer.parseInt(Count)];
    DownLoadJsonGL downLoadJsonGL = new DownLoadJsonGL();
    downLoadJsonGL.execute(JsonDataGL); //Here The NumberPicker is filled with the titles

}

Do you have any idea how can I do that? or lead me to tutorial online? Because I couldn't find anything concerns to this topic online. Thank you!


Solution

  • As the documentation for NumberPicker shows, you can use

    picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
                @Override
                public void onValueChange(NumberPicker picker, int oldVal, int newVal){
                    //Process the changes here
                }
        });