質問

I have got three NumberPickers and I would like to to detect when the value is changed in all of them. I currently use them for something else so I know they work. I tried to do it like a onClickListener when you switch the Id, but id doesn't work here

public class ..... implements OnValueChangeListener {

// Removed

NumberPicker np1;
// Get the id from xml
np1.setOnValueChangedListener(this);
NumberPicker np2;
// Get the id from xml
np2.setOnValueChangedListener(this);
NumberPicker np3;
// Get the id from xml
np3.setOnValueChangedListener(this);

@Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        // TODO Auto-generated method stub
        Log.i("value changed", "true"); // This IS shown
        switch (getView().getId()) { // I think this is wrong

        case R.id.numberPicker1:

            Log.i("value 1", "true"); // Not shown
            break;
        case R.id.numberPicker2s:

        Log.i("value 2", "true"); // Not shown
            break;
        case R.id.numberPicker3:

            Log.i("value 3", "true"); // Not shown
            break;

        }


    }

 }

I could add each listener separatley but I think that would be messy.

役に立ちましたか?

解決

You repeatedly set the listener only on your first number picker (np1)

EDIT:

Alright, I see. How about storing references to your NumberPicker objects in onCreate as member variables of your Activity and just match objects in your onValueChanged listener as follows:

public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
    if(picker == np1) {
      ...
    } else if(picker == np2) {
      ...
    } ...
}

I guess you're not retrieving the correct view's id in your code snippet above:

    switch (getView().getId()) { // I think this is wrong

You should call getId on the picker argument instead:

   switch(picker.getId())
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top