views:

401

answers:

3

Hi, Anyone please help me to pass String value from one screen to another screen in Blackberry

A: 

Copy and Paste? Howto

*Note: this is a joke.

asperous.us
+1  A: 

I think you may need to be a little more clear in what you require. But taking your original question literally, the following bit of code is how you would do it.

public class MyApp extends UiApplication {
  MyApp() {
    MyFirstScreen screenOne = new MyFirstScreen();
    pushScreen(screenOne);
    String str = screenOne.getWhateverStringINeed();
    MySecondScreen screenTwo = new MySecondScreen(str);
    pushScreen(screenTwo);
  }
}

The above code would push two screens onto the BlackBerry display stack, with the second screen essentially having the string (whatever string you happen to need), from the first screen.

Andrey Butov
+2  A: 

I would say to do pushing 2nd screen from the 1st screen, not from the application.
In app push first screen:

public class App extends UiApplication {
    public static void main(String[] args) {
     App app = new App();
     app.enterEventDispatcher();
    } 
    public App() {
     FirstScreen scr = new FirstScreen();
     pushScreen(scr);
    }
}

Second screen has a setter for string value:

public class SecondScreen extends MainScreen {

    String mTextValue = null;
    LabelField mLabel = null;

    public void setTextValue(String textValue) {
     mTextValue = textValue;
     mLabel.setText(mTextValue);
    }

    public SecondScreen() {
     super();  
     mLabel = new LabelField();
     add(mLabel);
    }
}

In first screen create second, set string value and push it. Pop first screen if you don't need to return on it:

public class FirstScreen extends MainScreen implements FieldChangeListener {

    BasicEditField mEdit = null; 
    ButtonField mButton = null;

    public FirstScreen() {
     super();    
      mEdit = new BasicEditField("input: ", "some text");
      add(mEdit);
      mButton = new ButtonField("Go second screen");
      mButton.setChangeListener(this);
      add(mButton);
    }
    public void fieldChanged(Field field, int context) {
     if(mButton == field)
     {
      SecondScreen scr = new SecondScreen();
      scr.setTextValue(mEdit.getText());
      UiApplication.getUiApplication().pushScreen(scr);
      UiApplication.getUiApplication().popScreen(this);
     }
    } 
}
Max Gontar