Question

I'm sort of confused as to how to implement a FieldChangeListener in the Blackberry JDE. One way has me make my main class implement FieldChangeListener, and then have a fieldchanged method inside of it, and another has me do:

    FieldChangeListener listenerUS = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("Something changed!");
pushScreen(_newScreen);
}
};

Either way, if I try to call a method (like pushScreen, or a custom method I've written), I get a runtime error. In debug mode, none of my print statements are being displayed, either. However, if I remove the fieldChanged method outright, it won't even compile, so I"m pretty sure it's seeing the code?

I've added the listener to the button I want it hooked up to either by having:

            but_temp.setChangeListener(this);

(in the first case) or by putting listenerUS.

Everything seems to be hooked up, but of my print statements show up, and if I call a method, I get a runtime error.

Does this make sense? Am I just completely confused about how to use listeners on the blackberry?

http://pastie.org/618950

There's a copy of my code as a whole...

Was it helpful?

Solution 3

I'm super confused, but I managed to fix things. I created a new class from scratch, and then just copied and pasted my old code into it. Everything works. The only thing I changed was only importing classes that Eclipse said was necessary (before I had some import statements from various tutorials, etc. so some were possibly not being used.)

Is it possible I was importing something that was causing things to crash?

I really would rather have most of my code in the screen itself, but trying that crashes the whole thing before I can even load. Something about the xml parser I'm using not being happy.

http://pastie.org/621932

There's the modified code. I'm really frustrated, because I know that there is some inherent UNDERSTANDING of this frame work that I'm not grokking, and that most of my troubles are coming from this. I suppose only practice will help me, though ^_^;;

OTHER TIPS

I looked at your code and nothing blatantly wrong jumped out at me. However, I wouldn't designate the main application class the duties of being the FieldChangeListener. It's not something it should have to be aware of. The best I can do for you is provide an example app that implements the FieldChangeListener interface for a ButtonField. It's not a solution but maybe with your better knowledge of your code you'll be able to pick something out that is different than this example. Hope it helps.

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.FieldChangeListener;

/**
 * Test implementation of ButtonField.
 */
public class TestAppMain extends UiApplication 
{
    /**
     * Default Constructor.
     */
    private TestAppMain() {        
        pushScreen(new AppScreen());
    }

    /**
     * App entry point.
     * @param args Arguments.
     */
    public static void main(String[] args) {
        TestAppMain app = new TestAppMain();
        app.enterEventDispatcher();
    }

    /**
     * Main application screen.
     */
    private static class AppScreen extends MainScreen 
    {
        /**
         * Default constructor.
         */
        public AppScreen() {
            LabelField title = new LabelField("Button Test Demo",
                    LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
            setTitle(title);

            // Create a button with a field change listener.
            FieldChangeListener listener = new FieldChangeListener() {
                public void fieldChanged(Field field, int context) {
                    ButtonField buttonField = (ButtonField) field;
                    System.out.println("Button pressed: " + buttonField.getLabel());
                }
            };
            ButtonField buttonField = new ButtonField("Test Button", ButtonField.CONSUME_CLICK);            
            buttonField.setChangeListener(listener);
            add(buttonField);
        }               

        /**
         * Handle app closing.
         */
        public void close() {
            Dialog.alert("Goodbye!");
            System.exit(0);
            super.close();
        }
    }
}

I agree with Fostah(+1), it's common to implement FieldChangeListener in Field, Manager or Screen, or use a standalone FieldChangeListener.
Also, to push/pull screen from Field:

UiApplication.getUiApplication().pushScreen(nextScreen);

See How to navigate back to the previous screen in the Blackberry emulator?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top