0

In my program I would like to use two sets of wheels like the following:

hours = (WheelView) findViewById(R.id.hour);
NumericWheelAdapter hourAdapter = new NumericWheelAdapter(this, 0, 23, "%02d");
hourAdapter.setItemResource(R.layout.wheel_text_item);
hourAdapter.setItemTextResource(R.id.text);
hours.setViewAdapter(hourAdapter);
hours.setCyclic(true);

These wheels have a scrollinglistener hours.addScrollingListener(scrolledListener);

The scrollinglistener looks like this:

// Wheel scrolled listener
OnWheelScrollListener scrolledListener = new OnWheelScrollListener() {
    public void onScrollingStarted(WheelView wheel) {
    }
    public void onScrollingFinished(WheelView wheel) {
    update();
    }
};

I would like to set one wheel with the value of the other but when I instantiate two listeners and scroll the first one it goes on setting wheel one and then wheel two and then wheel one again.

Is it possible to disable a one of the two scrolledListener?

I've tried this:

    hours.addScrollingListener(null);
    hours.setEnabled(false);

But this gave an error and the program had to be stopped.

Thanks for your help!

PS: the wheels are from * Android Wheel Control. * https://code.google.com/p/android-wheel/

Machavity
  • 30,841
  • 27
  • 92
  • 100
patrick
  • 1,282
  • 4
  • 21
  • 37

2 Answers2

0

From the sounds of it you are adding both OnWheelScrollListener to the same WheelView listener list which callsback to every listener it contains which would explain your behavior. The listeners should be added in a similar fashion to this...

hours = (WheelView) findViewById(R.id.hour);
        hours.addScrollingListener(new OnWheelScrollListener() {
            public void onScrollingStarted(WheelView wheel) {
            }
            public void onScrollingFinished(WheelView wheel) {
            update();
            }
        });

        hours2 = (WheelView) findViewById(R.id.hour2);
        hours2.addScrollingListener(new OnWheelScrollListener() {
            public void onScrollingStarted(WheelView wheel) {
            }
            public void onScrollingFinished(WheelView wheel) {
            update();
            }
        });

Also, you want to use the removeScrollingListener(OnWheelScrollListener) method to remove a listener...hours.addScrollingListener(null); is just adding a null item to your list which is causing your NullPointerException i presume.

Edit

    OnWheelScrollListener listener = new OnWheelScrollListener() {
                public void onScrollingStarted(WheelView wheel) {
                }
                public void onScrollingFinished(WheelView wheel) {
                if (wheel.getId() == R.id.hour) {
    //do wheel 1 logic
    } else if (wheel.getId() == R.id.hour2) {
    //do wheel 2 logic
    }
            };

hours.addScrollingListener(listener);
hours2.addScrollingListener(listener);
Joe
  • 4,801
  • 2
  • 16
  • 8
  • 2
    He could, though, add the same Listener to each view and run a check on which WheelView it is in the listener code. if(wheel == hours){} else if (wheel == hours2){} – Maximus Apr 20 '11 at 17:53
  • True, that looks like a more elegant solution than mine. – Joe Apr 20 '11 at 18:04
  • Thanks for your reply! I've tried the if constructions, but this still keeps updating first the first wheel and then the other one. Setting a wheel to a value will activate the listener. Maybe the listener does react different compared with other listeners. – patrick Apr 20 '11 at 18:39
  • Reference my edit for the implementation of Maximus' suggestion and ensure you're modeling yours appropriately – Joe Apr 20 '11 at 18:51
0

I've contacted the designer, Yuri, of the wheel that i'm using and he gave me the following solution which works:

// Wheel scrolled listener
OnWheelScrollListener scrolledListener = new OnWheelScrollListener() {
    boolean syncInProgress = false;

    public void onScrollingStarted(WheelView wheel) {
    }
    public void onScrollingFinished(WheelView wheel) {

        if (syncInProgress) {
            syncInProgress = false;
            return;
        }

        syncInProgress = true;
        if (wheel.getId() == R.id.wheel1) {
            // do something
        } else if (wheel.getId() == R.id.wheel2) {
            // do something else
        }
    }
};

Thank you all for your help!

patrick
  • 1,282
  • 4
  • 21
  • 37