Difference between revisions of "Lab BlackBerry Example"

From CDOT Wiki
Jump to: navigation, search
(3 code)
Line 92: Line 92:
 
}
 
}
 
</source>
 
</source>
 +
 +
The app has the MyApp class completed and it looks like:
 +
 +
<source lang="java">
 +
 +
package mypackage;
 +
 +
import net.rim.device.api.ui.Field;
 +
import net.rim.device.api.ui.FieldChangeListener;
 +
import net.rim.device.api.ui.UiApplication;
 +
import net.rim.device.api.ui.component.ButtonField;
 +
 +
public class MyApp extends UiApplication {
 +
 +
private MyScreen _screen;
 +
private ButtonField _nextScreenButton;
 +
 +
 +
public static void main(String[] args) {
 +
 +
MyApp theApp = new MyApp();
 +
theApp.enterEventDispatcher();
 +
}
 +
 +
 +
public MyApp() {
 +
// Push a screen onto the UI stack for rendering.
 +
_screen = new MyScreen();
 +
_nextScreenButton = new ButtonField("Go to Next Screen",
 +
ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
 +
 +
_nextScreenButton.setChangeListener(new FieldChangeListener() {
 +
 +
public void fieldChanged(Field field, int context) {
 +
pushScreen(new NextScreen());
 +
 +
}
 +
});
 +
 +
_screen.add(_nextScreenButton);
 +
pushScreen(_screen);
 +
}
 +
}
 +
 +
</source>
 +
 +
-----------------

Revision as of 21:58, 26 February 2011

This lab example allows you to understand the process of building a BlackBerry application using Java Plug-in for Eclipse

Step 1.

File->New->Others->BlackBerry Project

Note: If you want really to learn how to build a BB app, then in the wizard choose the Empty Application option.

The structure of BB project will be defined, but as the level on code generation, one will get only an empty Java app, with a code similar with this:

package mypackage;

public class MyApp {
    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */ 
    public static void main(String[] args){
    }
}

You have to extend UiApplication. This class extends the UiApplication class, providing a graphical use interface. You have to import import net.rim.device.api.ui.UiApplication;

Let us suppose that you want your application to have two screens and you want to switch from a screen to another using a button. Thus, you have to define a variable of type Screen to keep track of the Screen object and a variable of type ButtonField.

You need also to importi net.rim.device.api.ui.component.ButtonField;


Step 2.

In the main method create a new instance of the application and make the currently running thread the application's event dispatch thread.

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;

     public class MyApp extends UiApplication {
         private MyScreen _screen;
         private ButtonField _nextScreenButton;

         public static void main(String[] args) {
             MyApp theApp = new MyApp();
             theApp.enterEventDispatcher();
}

Step 3.

When the constructor of the MyApp class is defined, the graphical objects must be build.

Thus, _screen object will be an instance of a Screen class. The _nextScreenButton object will be an instance of ButtonField class.

In the BB app any field object could specify a listener for changes to this field by invoking the method

public void setChangeListener(FieldChangeListener listener) Each field can have only one change listener object. The param listener is the object to listen for field changes.

The FieldChangeListener is the istener interface for handling Field change events. The interface has only one method:

void fieldChanged(Field field, int context)

We want in our app, when the user click the button the method to create a new Screen object and to push it to the user through the invocation of

pushScreen(new NextScreen()); where the NextScreen is a class that will define the next screen of the app.

Thus the constructor of our app will look like:

	public MyApp() {
		// Push a screen onto the UI stack for rendering.
		_screen = new MyScreen();
		_nextScreenButton = new ButtonField("Go to Next Screen",
				ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
		
		_nextScreenButton.setChangeListener(new FieldChangeListener() {
			
			public void fieldChanged(Field field, int context) {
				pushScreen(new NextScreen());
				
			}
		});
		
		_screen.add(_nextScreenButton);
		pushScreen(_screen);
	}

The app has the MyApp class completed and it looks like:

package mypackage;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;

public class MyApp extends UiApplication {

	private MyScreen _screen;
	private ButtonField _nextScreenButton;


	public static void main(String[] args) {
		
		MyApp theApp = new MyApp();
		theApp.enterEventDispatcher();
	}

	
	public MyApp() {
		// Push a screen onto the UI stack for rendering.
		_screen = new MyScreen();
		_nextScreenButton = new ButtonField("Go to Next Screen",
				ButtonField.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
		
		_nextScreenButton.setChangeListener(new FieldChangeListener() {
			
			public void fieldChanged(Field field, int context) {
				pushScreen(new NextScreen());
				
			}
		});
		
		_screen.add(_nextScreenButton);
		pushScreen(_screen);
	}
}