Open a new mainscreen within a mainscreen in blackberry - blackberry

I'm making an application in which there are few tabs on each tab click a url is called and an online xml is parsed. The data from this xml is displayed. The problem is that when the data is dispalyed the tabs disappear and only appear when i press the back button of the simulator. Whereas the data should appear below the tabs.
Please help
My UI
public class TabControl extends UiApplication {
public TabControl() {
TabControlScreen screen = new TabControlScreen();
pushScreen(screen);
}
public static void main(String[] args) {
TabControl app = new TabControl();
app.enterEventDispatcher();
}
private class TabControlScreen extends MainScreen implements FocusChangeListener {
private Bitmap backgroundBitmap = Bitmap.getBitmapResource("rednavnar.png");
private LabelField tab1;
private LabelField tab2;
private LabelField tab3;
private VerticalFieldManager tabArea;
private VerticalFieldManager tab1Manager;
private VerticalFieldManager tab2Manager;
private VerticalFieldManager tab3Manager;
public TabControlScreen() {
LabelField appTitle = new LabelField("Energy", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH | LabelField.FIELD_HCENTER);
this.setTitle(appTitle);
HorizontalFieldManager hManager = new HorizontalFieldManager( Manager.HORIZONTAL_SCROLL | Manager.HORIZONTAL_SCROLLBAR | Manager.USE_ALL_WIDTH | Manager.TOPMOST) {
// Override the paint method to draw the background image.
public void paint(Graphics graphics) {
// Draw the background image and then call paint.
graphics.drawBitmap(0, 0, 700, 100, backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
tab1 = new LabelField("Top News", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
protected boolean navigationClick(int status,int time){
tabArea = displayTab1();
return true;
}
};
LabelField separator = new LabelField("|", LabelField.NON_FOCUSABLE);
tab2 = new LabelField("Power", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
protected boolean navigationClick(int status,int time){
tabArea = displayTab2();
return true;
}
};
LabelField separator1 = new LabelField("|", LabelField.NON_FOCUSABLE);
tab3 = new LabelField("Renewable Energy", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
protected boolean navigationClick(int status,int time){
tabArea = displayTab3();
return true;
}
};
tab1.setFocusListener(this);
tab2.setFocusListener(this);
tab3.setFocusListener(this);
hManager.add(tab1);
hManager.add(separator);
hManager.add(tab2);
hManager.add(separator1);
hManager.add(tab3);
add(hManager);
add(new SeparatorField());
tab1Manager = new VerticalFieldManager();
tab2Manager = new VerticalFieldManager();
tab3Manager = new VerticalFieldManager();
tabArea = displayTab1();
add(tabArea);
}
public void focusChanged(Field field, int eventType) {
if (tabArea != null) {
if (eventType == FOCUS_GAINED) {
if (field == tab1) {
delete(tabArea);
tabArea = displayTab1();
add(tabArea);
} else if (field == tab2) {
delete(tabArea);
tabArea = displayTab2();
add(tabArea);
} else if (field == tab3) {
delete(tabArea);
tabArea = displayTab3();
add(tabArea);
}
}
}
}
public VerticalFieldManager displayTab1() {
UiApplication.getUiApplication().pushScreen(new News());
return tab1Manager;
}
public VerticalFieldManager displayTab2() {
UiApplication.getUiApplication().pushScreen(new Power());
return tab2Manager;
}
public VerticalFieldManager displayTab3() {
UiApplication.getUiApplication().pushScreen(new Energy());
return tab3Manager;
}
}
}
My News Main Screen
public class News extends MainScreen {
public News() {
String xmlUrl = "http://182.71.5.53:9090/solr/core4/select/?q=*";
String[][] urlData = XmlFunctions.getURLFromXml(xmlUrl);
for (int i = 0; i < urlData.length; i++) {
final String title = urlData[0][i];
final String id = urlData[1][i];
//add(new LinkLabel(title, i));
add(new RichTextField(title){
protected boolean navigationClick(int status,int time){
String cId = id;
String bodyUrl = "http://192.168.1.44:9090/solr/core0/select/?q="+cId+";
String[][] bodyData = XmlFunctions.getBodyFromXml(bodyUrl);
for(int j=0;j<bodyData.length;j++){
String body = bodyData[0][j];
add(new RichTextField(body));
}
return true;
}
});
add(new SeparatorField());
}
}
}
Now i want to open this news screen below the tabs
Please help

For your TabControl Class
public class TabControl extends UiApplication {
public TabControl() {
TabControlScreen screen = new TabControlScreen();
pushScreen(screen);
}
public static void main(String[] args) {
TabControl app = new TabControl();
app.enterEventDispatcher();
}
private class TabControlScreen extends MainScreen {
private Bitmap backgroundBitmap = Bitmap.getBitmapResource("rednavnar.png");
private LabelField tab1;
private LabelField tab2;
private LabelField tab3;
private VerticalFieldManager tabArea;
public TabControlScreen() {
LabelField appTitle = new LabelField("Energy", LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH | LabelField.FIELD_HCENTER);
this.setTitle(appTitle);
HorizontalFieldManager hManager = new HorizontalFieldManager( Manager.HORIZONTAL_SCROLL | Manager.HORIZONTAL_SCROLLBAR | Manager.USE_ALL_WIDTH | Manager.TOPMOST) {
// Override the paint method to draw the background image.
public void paint(Graphics graphics) {
// Draw the background image and then call paint.
graphics.drawBitmap(0, 0, 700, 100, backgroundBitmap, 0, 0);
super.paint(graphics);
}
};
tab1 = new LabelField("Top News", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
protected boolean navigationClick(int status,int time){
delete(tabArea);
tabArea = displayTab1();
add(tabArea);
return true;
}
};
LabelField separator = new LabelField("|", LabelField.NON_FOCUSABLE);
tab2 = new LabelField("Power", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
protected boolean navigationClick(int status,int time){
delete(tabArea);
tabArea = displayTab2();
add(tabArea);
return true;
}
};
LabelField separator1 = new LabelField("|", LabelField.NON_FOCUSABLE);
tab3 = new LabelField("Renewable Energy", LabelField.FOCUSABLE | LabelField.HIGHLIGHT_FOCUS){
protected boolean navigationClick(int status,int time){
delete(tabArea);
tabArea = displayTab3();
add(tabArea);
return true;
}
};
hManager.add(tab1);
hManager.add(separator);
hManager.add(tab2);
hManager.add(separator1);
hManager.add(tab3);
add(hManager);
add(new SeparatorField());
// USELESS CODE HAS BEEN REMOVED
tabArea = displayTab1();
add(tabArea);
}
public VerticalFieldManager displayTab1() {
return new News(); // RETURN THE MANAGER DIRECTLY
}
public VerticalFieldManager displayTab2() {
return new Power(); // RETURN THE MANAGER DIRECTLY
}
public VerticalFieldManager displayTab3() {
return new Energy(); // RETURN THE MANAGER DIRECTLY
}
}
}
For your News class
public class News extends VerticalFieldManager { // CHANGING THE EXTENSION SUPER CLASS
public News() {
super(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR);
String xmlUrl = "http://182.71.5.53:9090/solr/core4/select/?q=*";
String[][] urlData = XmlFunctions.getURLFromXml(xmlUrl);
for (int i = 0; i < urlData.length; i++) {
final String title = urlData[0][i];
final String id = urlData[1][i];
//add(new LinkLabel(title, i));
add(new RichTextField(title){
protected boolean navigationClick(int status,int time){
String cId = id;
String bodyUrl = "http://192.168.1.44:9090/solr/core0/select/?q="+cId+";
String[][] bodyData = XmlFunctions.getBodyFromXml(bodyUrl);
for(int j=0;j<bodyData.length;j++){
String body = bodyData[0][j];
add(new RichTextField(body));
}
return true;
}
});
add(new SeparatorField());
}
}
}

Yes, create a seprate mainscreen or screen objects and from your parent screen you can execute the below code to open new screen using the below code:
UIApplication.getUiApplication().pushScreen(your custom screen object);
Thanks

You don't need to create 2 mainscreens in the scenario which you described.
Just create a VerticalFieldManager, add it to the mainscreen, then add content to it.
Here's a sample of code that can help you, apply it in your mainscreen constructor.
VerticalFieldManager VFM_Content = new VerticalFieldManager();
add(VFM_Content);
VFM_Content.add(/*put content here embedded in any field type you prefer*/);

Related

How to set Dialog YES, NO button action

I am trying to set manually what happens when i click YES or NO button in Dialog. There is my code. (Sorry i am not good in english)
Dialog D = new Dialog(Dialog.D_YES_NO, "Comprar Recarga", 0, Bitmap.getBitmapResource("icon.png"),Dialog.EDITABLE);
D.add(List_tipo);
D.add(List_valor);
D.add(List_num_conta);
D.show();
if(D.getSelectedValue()==Dialog.OK)
{
int Recarga, num_conta;
Recarga = Integer.parseInt(List_valor.getChoice(List_valor.getSelectedIndex()).toString());
num_conta = Integer.parseInt(List_num_conta.getChoice(List_num_conta.getSelectedIndex()).toString());
int tipo = List_tipo.getSelectedIndex();
if(tipo==0)
{
recargas_existentes.comprar_mcel(Recarga, num_conta);
}
if(tipo==1)
{
recargas_existentes.comprar_vodacom(Recarga, num_conta);
}
if(tipo==2)
{
recargas_existentes.comprar_bla_bla(Recarga, num_conta);
}
}
you should be able to use Dialog's doModal() method.
Here's a good example:
https://stackoverflow.com/a/10681171/2415100
If you can't use doModal, try this:
public class ProceedPrompt extends PopupScreen
{
int response = Dialog.NO;
private ProceedPrompt(String message)
{
super(new VerticalFieldManager());
LabelField labelField = new LabelField(message, USE_ALL_WIDTH | DrawStyle.HCENTER);
add(labelField);
ButtonField button_proceed = new ButtonField(
"Proceed", USE_ALL_WIDTH | FIELD_HCENTER)
{
protected boolean navigationClick(int status, int time)
{
response = Dialog.YES;
close();
return true;
}
};
ButtonField button_cancel = new ButtonField(
"Cancel", USE_ALL_WIDTH | FIELD_HCENTER)
{
protected boolean navigationClick(int status, int time)
{
response = Dialog.NO;
close();
return true;
}
};
add(button_proceed);
add(button_cancel);
}
public static int doModal(String s)
{
ProceedPrompt prompt = new ProceedPrompt(s);
UiApplication.getUiApplication().pushModalScreen(prompt);
return prompt.response;
}
}
Then call with
int i = ProceedPrompt.doModal("Did this work?");

How to reset scroll values, NO_SCROLL_RESET, Blackberry

I need to reset the values from scrolling, I has implemented a tabbar (developing in Blackberry OS 6), with Vertical Scroll in VerticalFieldManager, but when I select another field(tab) the Vertical Scroll return me a nullpointer, the tab code is fine, so I guess is because the first scroll event saved the items lenght from the first field (tab) and when the lenght is less than the first one give me a nullpointer, I need Reset the values from scroll when I finish scrolling, I saw this
(Mannager.NO_SCROLL_RESET)
but it's deprecated. I'm using OS 6 but the tabbar must work for every devices.
public class CustomizeTabbar extends MainScreen {
private int Resolucion;
VerticalFieldManager lista;
private AbsoluteFieldManager tab1Manager;
private AbsoluteFieldManager tab2Manager;
private AbsoluteFieldManager tab3Manager;
private AbsoluteFieldManager tab4Manager;
private AbsoluteFieldManager tab5Manager; ///Detalle Nota
private AbsoluteFieldManager tabArea;
private String Token;
public CustomizeTabbar(int Res, String token) {
Resolucion=Res;
this.Token=token;
try{
///Cabecera Tabbar
HorizontalFieldManager htabbar= new HorizontalFieldManager(NO_HORIZONTAL_SCROLL|NO_VERTICAL_SCROLL|USE_ALL_WIDTH ){
//define width
public int getPreferredWidth()
{
if(Resolucion==1)
return 320;
else
return 480;
}
//define height
public int getPreferredHeight()
{
if(Resolucion==1)
return 25;
else
return 36;
}
protected void sublayout( int maxWidth, int maxHeight )
{
super.sublayout(getPreferredWidth(),
getPreferredHeight());
setExtent(getPreferredWidth(), getPreferredHeight());
}
};
final BitmapButtonField tab1= new BitmapButtonField(Bitmap.getBitmapResource("1notasactuales.png"),Bitmap.getBitmapResource("1notasactualesfocus.png"),FOCUSABLE);
final BitmapButtonField tab2= new BitmapButtonField(Bitmap.getBitmapResource("1inasistencia.png"),Bitmap.getBitmapResource("1inasistenciafocus.png"),FOCUSABLE);
final BitmapButtonField tab3= new BitmapButtonField(Bitmap.getBitmapResource("1Horario.png"),Bitmap.getBitmapResource("1HorarioFocus.png"),FOCUSABLE);
final BitmapButtonField tab4= new BitmapButtonField(Bitmap.getBitmapResource("1record.png"),Bitmap.getBitmapResource("1recordfocus.png"),FOCUSABLE);
LabelField spacer1 = new LabelField(" | ", LabelField.NON_FOCUSABLE);
LabelField spacer2 = new LabelField(" | ", LabelField.NON_FOCUSABLE);
LabelField spacer3 = new LabelField(" | ", LabelField.NON_FOCUSABLE);
///Controlador del Tabbar
FieldChangeListener click=new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
lista=null;
if(tabArea!=null){
try{
if(field==tab1)
{
delete(tabArea);
tabArea=NotasActualesDisplay();
add(tabArea);
}else
if(field==tab2)
{
delete(tabArea);
tabArea = InasistenciaDisplay();
add(tabArea);
}else
if(field==tab3)
{
delete(tabArea);
tabArea = HorarioDisplay();
add(tabArea);
}else
if(field==tab4)
{
delete(tabArea);
tabArea=RecordAcademicoDisplay();
add(tabArea);
}
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
};
tab1.setChangeListener(click);
tab2.setChangeListener(click);
tab3.setChangeListener(click);
tab4.setChangeListener(click);
htabbar.add(tab1);
htabbar.add(spacer1);
htabbar.add(tab2);
htabbar.add(spacer2);
htabbar.add(tab3);
htabbar.add(spacer3);
htabbar.add(tab4);
Background bg = BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("2tabbar_pages.png"));
htabbar.setBackground(bg);
setTitle(htabbar);
/////////
tabArea = NotasActualesDisplay();
add(tabArea);
}catch (Exception e) {
// TODO: handle exception
}
}
public AbsoluteFieldManager NotasActualesDisplay() {
final VerticalFieldManager lista=new VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR|Manager.NO_SCROLL_RESET){
/* Here I design the elements with JSON using FOR*/
}
return tab1Manager;
}
public AbsoluteFieldManager InasistenciaDisplay() {
VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR|Manager.NO_SCROLL_RESET){
/* Here I design the elements with JSON using FOR*/
}
return tab2Manager;
}
public AbsoluteFieldManager HorarioDisplay() {
VerticalFieldManager(Manager.VERTICAL_SCROLL|Manager.VERTICAL_SCROLLBAR|Manager.NO_SCROLL_RESET){
/* Here I design the elements with JSON using FOR*/
}
}
return tab3Manager;
}

Blackberry Tab bar control

I want a tab bar in my application.I tried the sample code for it in blackberry but I want it at the bottom of the screen.I tried this code but it gives me uncaught runtime exception.Why this so? Whats the problem in this code?
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.image.*;
import net.rim.device.api.ui.toolbar.*;
import net.rim.device.api.util.*;
import net.rim.device.api.system.*;
import net.rim.device.api.command.*;
public class ToolbarDemo extends UiApplication
{
public static void main(String[] args)
{
ToolbarDemo theApp = new ToolbarDemo();
theApp.enterEventDispatcher();
}
public ToolbarDemo()
{
// Push a screen onto the UI stack for rendering.
pushScreen(new ToolbarDemoScreen());
}
private final static class ToolbarDemoScreen extends MainScreen
{
public ToolbarDemoScreen()
{
if (ToolbarManager.isToolbarSupported())
{
setTitle("Toolbar Demo");
ToolbarManager manager = new ToolbarManager();
setToolbar(manager);
try
{
Bitmap myBitmap = Bitmap.getBitmapResource("myImg.jpg");
Image myImage = ImageFactory.createImage(myBitmap);
/*
* To create more buttons, Repeat the following lines
* up until manager.add()
*/
ToolbarButtonField button1 = new ToolbarButtonField(myImage, new StringProvider("butn1"));
button1.setCommandContext(new Object()
{
public String toString()
{
return "Button1";
}
});
button1.setCommand(new Command(new CommandHandler()
{
public void execute(ReadOnlyCommandMetadata metadata, Object context)
{
Dialog.alert("Executing command for " + context.toString());
}
}));
manager.add(new ToolbarSpacer(0));
manager.add(button1);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
else
{
Dialog.alert("The Toolbar is not supported on this device.");
}
}
}
}
If you are getting the Exception, Uncaught Exception: pushModalScreen called by a non-event thread then you can try following code snippet for displaying an alert.
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert("The Toolbar is not supported on this device.");
}
});
And please check this question, “pushModalScreen called by a non-event thread” thrown on event thread.
Try this code
EncodedImage e1 = EncodedImage.getEncodedImageResource("home.png");
EncodedImage e2 = EncodedImage.getEncodedImageResource("map.png");
EncodedImage e3 = EncodedImage.getEncodedImageResource("members.png");
EncodedImage e4 = EncodedImage.getEncodedImageResource("message.png");
EncodedImage e5 = EncodedImage.getEncodedImageResource("settings.png");
EncodedImage e6 = EncodedImage.getEncodedImageResource("logout.png");
HorizontalFieldManager buttonPanel = new HorizontalFieldManager(Field.FIELD_TOP | Field.USE_ALL_WIDTH);
VerticalFieldManager vfm=new VerticalFieldManager();
vfm.add(new LabelField("Map"));
vfm.add(new BitmapField(e2.getBitmap(),FOCUSABLE){
protected boolean navigationClick(int status, int time){
Dialog.alert("MAP");
return true;
}
});
buttonPanel.add(vfm);
buttonPanel.add(new LabelField(" "));
VerticalFieldManager vfm1=new VerticalFieldManager();
vfm1.add(new LabelField("Members"));
vfm1.add(new BitmapField(e3.getBitmap(),FOCUSABLE){
protected boolean navigationClick(int status, int time){
Dialog.alert("Members");
return true;
}
});
buttonPanel.add(vfm1);
VerticalFieldManager vfm2=new VerticalFieldManager();
vfm2.add(new LabelField("Message"));
vfm2.add(new BitmapField(e4.getBitmap(),FOCUSABLE){
protected boolean navigationClick(int status, int time){
Dialog.alert("Message");
return true;
}
});
buttonPanel.add(vfm2);
VerticalFieldManager vfm3=new VerticalFieldManager();
vfm3.add(new LabelField("Settings"));
vfm3.add(new BitmapField(e5.getBitmap(),FOCUSABLE){
protected boolean navigationClick(int status, int time){
Dialog.alert("Settings");
return true;
}
});
buttonPanel.add(vfm3);
VerticalFieldManager vfm4=new VerticalFieldManager();
vfm4.add(new LabelField("Logout"));
vfm4.add(new BitmapField(e6.getBitmap(),FOCUSABLE){
protected boolean navigationClick(int status, int time){
Dialog.alert("Logout");
return true;
}
});
buttonPanel.add(vfm4);
setStatus(buttonPanel);

Issue in using a custom bitmap field

I want to display an image at the bottom of the screen and make it clickable ,i have created a custom bitmap field ImageButtonField and trying to use it ,but i am getting ImageButtonField cannot be resolved.
public class ImageButtonField extends BitmapField
{
public ImageButtonField(Bitmap image) {
super(image);
}
public ImageButtonField(Bitmap image,Field location) {
//super(image,location);
}
public boolean isFocusable() {
return true;
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean trackwheelClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time) {
if(Characters.ENTER == character || Characters.SPACE == character) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}
public NativeScreen() {
super();
LabelField title = new LabelField("Calendar DatePicker",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
hrzManager = new HorizontalFieldManager() {
protected void paintBackground(Graphics graphics) {
graphics.setBackgroundColor(0x0007F5ED);
graphics.clear();
super.paint(graphics);
}
};
hrzManager.add(title);
this.add(hrzManager);
//The background image.
backgroundBitmap = Bitmap.getBitmapResource("cryptodemo_jde.png");
// MainScreen mainScreen = new MainScreen(MainScreen.NO_VERTICAL_SCROLL | MainScreen.NO_HORIZONTAL_SCROLL);
VerticalFieldManager verticalFieldManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL |
Manager.USE_ALL_HEIGHT | Manager.USE_ALL_WIDTH ) ;
BasicEditField bef = new BasicEditField("To: ","",50,BasicEditField.FILTER_EMAIL);
//The LabelField will show up through the transparent image.
LabelField labelField = new LabelField("This is a label");
HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH | HorizontalFieldManager.USE_ALL_HEIGHT);
//A bitmap field with a transparent image.
//The background image will show up through the transparent BitMapField image.
ImageButtonField bitmapField = new ImageButtonField(Bitmap.getBitmapResource("pimdemo_jde.png"), Field.FIELD_BOTTOM);
horizontalFieldManager.add(bitmapField);
BitmapField bitmapField1 = new BitmapField(Bitmap.getBitmapResource("attachmentdemo_jde.png"), Field.FIELD_BOTTOM);
horizontalFieldManager.add(bitmapField1);
//Add the fields to the manager.
// verticalFieldManager.add(bef);
// verticalFieldManager.add(labelField);
verticalFieldManager.add(horizontalFieldManager);
//Add the manager to the screen.
this.add(verticalFieldManager);
There's no constructor in your class that lets you do this:
ImageButtonField ImageButtonField bitmapField = new ImageButtonField(Bitmap.getBitmapResource("pimdemo_jde.png"), Field.FIELD_BOTTOM);
The constructor that you're trying to use requires a Bitmap instance and a long primitive (for Field.FIELD_BOTTOM). You should add a constructor with the following signature for your code to work:
public ImageButtonField (Bitmap bitmap, long style){
super(bitmap, style);
}

Get field value from PopupScreen back to MainScreen in Blackberry

Now i have a MyScreen which extends from MainScreen containing a button. When the button clicked, it will show the PopupScreen containing list of RadioButtonFields, OK and Cancel buttons. My question is how to get RadioButtonField value from PopupScreen back to MyScreen.
Please help me in this case.
Thanks
Below is my code
MyScreen.java
public class MyScreen extends MainScreen{
private int currentValue = 0;
public MyScreen() {
LabelField lblField = new LabelField();
lblField.setText("Current Value = " + currentValue);
ButtonField btnPopup = new ButtonField("Show Popup");
btnPopup.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(new MyPopupScreen());
}
});
add(lblField);
add(btnPopup);
}
}
MyPopupScreen
public class MyPopupScreen extends PopupScreen implements FieldChangeListener {
private int currentValue = -1;
public MyPopupScreen() {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR), Field.FOCUSABLE
);
add(new LabelField("Select Chart Type"));
RadioButtonGroup radioGroup = new RadioButtonGroup();
for (int i = 0; i < 10; i++) {
RadioButtonField radio = new RadioButtonField("Option no. " + i, radioGroup, false);
radio.setChangeListener(this);
add(radio);
}
ButtonField cancelBtn = new ButtonField("Close");
ButtonField okBtn = new ButtonField("OK");
add(okBtn);
add(cancelBtn);
cancelBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().popScreen();
}
});
okBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
//How to get radio value or index value to pass back to MyScreen
}
});
}
public void fieldChanged(Field field, int context) {
if (field instanceof RadioButtonField) {
RadioButtonField radio = (RadioButtonField) field;
currentValue = radio.getIndex();
}
}
}
As Vijay, suggested this is one way that we could achieve this using inner class. The other approach could be, using design observer design patterns.
Try understanding the below code, and use what ever is efficient for you.
MyScreen.java
public class MyScreen extends MainScreen implements MyListener {
MyScreen screen = null;
public MyScreen() {
screen = this;
LabelField lblField = new LabelField();
lblField.setText("Current Value = " + currentValue);
ButtonField btnPopup = new ButtonField("Show Popup");
btnPopup.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(new MyPopupScreen(screen));
}
});
add(lblField);
add(btnPopup);
}
public void valueChanged(int selectedIdx) {
Dialog.alert(selectedIdx );
}
}
MyListener.java
public interface MyListener {
public void valueChanged(int selectedIdx);
}
MyPopupScreen.java
public class MyPopupScreen extends PopupScreen implements FieldChangeListener {
MyScreen deligate = null;
public MyPopupScreen(final MyScreen deligate) {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR), Field.FOCUSABLE);
this.deligate = deligate;
add(new LabelField("Select Type"));
ButtonField closeBtn = new ButtonField("Close");
ButtonField okBtn = new ButtonField("OK");
add(okBtn);
add(closeBtn);
closeBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
});
okBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
deligate.valueChanged(radioGroup.getSelectedIndex());
UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
}
});
}
}
public class MyScreen extends MainScreen{
private int currentValue = 0;
String result; // Declare the result
public MyScreen() {
LabelField lblField = new LabelField();
lblField.setText("Current Value = " + currentValue);
ButtonField btnPopup = new ButtonField("Show Popup");
btnPopup.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().pushScreen(new MyPopupScreen());
}
});
ButtonField btnGetResult = new ButtonField("result"); //Add new Button
btnGetResult.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
Dialog.inform(result);
}
});
add(lblField);
add(btnPopup);
add(btnGetResult);// Add button to get the selected index
}
public class MyPopupScreen extends PopupScreen implements FieldChangeListener {
private int currentValue = -1;
RadioButtonField radio;
public MyPopupScreen() {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL|VerticalFieldManager.VERTICAL_SCROLLBAR), Field.FOCUSABLE
);
add(new LabelField("Select Chart Type"));
final RadioButtonGroup radioGroup = new RadioButtonGroup();
for (int i = 0; i < 10; i++) {
radio = new RadioButtonField("Option no. " + i, radioGroup, false);
radio.setChangeListener(this);
add(radio);
}
ButtonField cancelBtn = new ButtonField("Close");
ButtonField okBtn = new ButtonField("OK");
add(okBtn);
add(cancelBtn);
cancelBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().popScreen(MyPopupScreen.this);
}
});
okBtn.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
result = radioGroup.getSelectedIndex()+"";
UiApplication.getUiApplication().popScreen(MyPopupScreen.this);
}
});
}
public void fieldChanged(Field field, int context) {
if (field instanceof RadioButtonField) {
RadioButtonField radio = (RadioButtonField) field;
currentValue = radio.getIndex();
}
}
}
}

Resources