弹出屏幕关闭后如何调用MainScreen的构造函数

时间:2012-03-21 10:21:31

标签: blackberry

我正在调用一个弹出屏幕,我在其中有objectchoicefield,我从下拉列表中选择一个数据并在静态变量中设置值。我需要在调用popupscreen的mainscreen的构造函数中读取此值。

如何做到这一点?我希望使用我现在选择的数据重新加载相同的屏幕。

最初当我启动应用程序时,我看到Ascreen带有一些数据,一旦我点击图像我调用一个弹出屏幕,我选择一些数据,现在我想用弹出屏幕关闭时重新加载带有所选值的AScreen

Ascreen

Public AScreen(){
    String timezone_static = MyPopup.ocfDATA;
}
if (field == bitmapField1) {

    UiApplication.getUiApplication().pushScreen(new MyPopup(screen));

}

public class MyPopup extends PopupScreen {
    ObjectChoiceField ocf;
    NativeScreen deligate = null;
    public static String ocfDATA;

    public MyPopup(final NativeScreen deligate) {

        super(new VerticalFieldManager(), Field.FOCUSABLE);
        this.deligate = deligate;
        // this listener 'listens' for any event (see at the bottom)
        FieldListener listener = new FieldListener();

        TimeZone[] zone = TimeZoneUtilities.getAvailableTimeZones();
        ocf = new ObjectChoiceField("Set Timezone", zone);
        ocf.setChangeListener(listener);
        add(ocf);
    }

    class FieldListener implements FieldChangeListener {
        public void fieldChanged(Field f, int context) {

            if (f == ocf) {

                Object ob = ocf.getChoice(ocf.getSelectedIndex());
                String str = ob.toString();
                ocfDATA = str;
                deligate.valueChanged(str);
                Dialog.alert(str);
                close();
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

我正在解释一些流程如何从一个屏幕导航另一个屏幕的值。 这是主屏幕中的简单方法

protected void onExposed() {
        String timezone_static = MyPopup.ocfDATA;
    }

否则你想知道更多从一个地方导航到另一个地方只是理解下面的代码

package mypackage;

import java.util.TimeZone;

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;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.util.TimeZoneUtilities;

public final class MyScreen extends MainScreen implements FieldChangeListener
{
    private ButtonField btn;
    private LabelField zone_name;
    public MyScreen()
    { 
        //may be you have lots of parameters in constructers those all are in live state only design values you can delete

        CreateGui("Default Zone");// add all your design in this function 
    }
    public void CreateGui(String selcetd_time_zone)
    {
        setTitle(selcetd_time_zone);
        zone_name=new LabelField(selcetd_time_zone, Field.FOCUSABLE);
        btn=new ButtonField("select timezone",Field.FOCUSABLE);
        btn.setChangeListener(this);
        add(btn);
        add(zone_name);
    }
    public void ReloadScreenAccordingToNewZone(String zone_name)
    {
        synchronized (UiApplication.getEventLock()) {
            deleteAll();//clean your GUI
            invalidate();
            CreateGui(zone_name);
        }

    }
    public void fieldChanged(Field field, int context) {
        if(field==btn)
        {
             UiApplication.getUiApplication().pushScreen(new MyPopup(MyScreen.this));
        }
    }
}
class MyPopup extends PopupScreen implements FieldChangeListener{
    ObjectChoiceField ocf;
    MyScreen deligate = null;
    public MyPopup(MyScreen deligate) {
        super(new VerticalFieldManager(), Field.FOCUSABLE);
        this.deligate = deligate;
        TimeZone[] zone = TimeZoneUtilities.getAvailableTimeZones();
        ocf = new ObjectChoiceField("Set Timezone", zone);
        ocf.setChangeListener(this);
        add(ocf);
    }
    public void fieldChanged(Field field, int context) {
        if(field==ocf)
        {
            Object ob = ocf.getChoice(ocf.getSelectedIndex());
            deligate.ReloadScreenAccordingToNewZone(ob.toString());
            close();
        }

    }

}

我的建议是,如果你想从另一个类更新更多设计,那么只需将构造函数和设计分开

所有设计都在一个方法中编写,假设CreateGui();从constructer调用此方法; 如果要重新加载更新设计,只需从onExposed()方法

调用CreateGui

答案 1 :(得分:0)

protected void onExposed()
{
     invalidate();
    //set your popup selected value at the palace where you want to set it

 }

答案 2 :(得分:0)

以下是您的屏幕。

  1. MainScreen
  2. PopupScreen
  3. 当您关闭PopupScreen时,onExposed()的{​​{1}}方法将被调用。

    如果您将MainScreenString传递到主屏幕PopupScreen静态than you have to take one PopupScreen variable in弹出屏and setvalue of it .When u close onExposed()than MainScreen method of onExposed()will be called. you have to access it as static way like that in String getStringfromPopup = PopupScreen.yourStaticString;`

    希望它的帮助...