1

我正在使用 javafx gluon 和 GCM 开发移动应用程序进行消息传递

当其他设备广播消息时,我想通过调用其控制器来刷新带有传入消息的当前活动视图。

我正在使用Afterburner框架。它还在胶子入门中进行了描述

如何从扩展 MobileApplication 的类中的 getView() 检索视图控制器?

这是我创建的类:

public class MyGCMListenerService extends GcmListenerService {
private final String NOTIFICATION_TAG = "NotificationExample";

public MyGCMListenerService() {
}

@Override
public void onMessageReceived(String from, Bundle data) {
String varMessage = data.getString("message");
    try {
        JSONObject json = new JSONObject(varMessage);

        String messageContent = getStringFromJSON(json, "message");
        Integer senderId = getIntegerFromJSON(json, "senderId");
        String senderName = getStringFromJSON(json, "senderName");
        String comId = getStringFromJSON(json, "communityId");

        SKSApplication.getInstance().doRefreshMessageUI(messageContent,senderId,senderName,comId );
    } catch (JSONException e) {
        e.printStackTrace();
    }
}}

布局 xml 文件 (directmessage.fxml)

<View xmlns:fx="http://javafx.com/fxml/1" fx:id="directMessageView" prefHeight="600.0" prefWidth="400.0"
  xmlns="http://javafx.com/javafx/8.0.40"
  fx:controller="com.tenma.mobile.message.directmessage.DirectMessagePresenter">
</View>

DirectMessageView 类扩展了 FXMLView

public class DirectMessageView extends FXMLView {
}

类 DirectMessagePresenter 实现 Initializable

public class DirectMessagePresenter implements Initializable{
    public void displayIncomingMessage(String messageContent,Integer    senderId,String senderName, String comId){
    //save to internal db
    // display message
    }
}

扩展 MobileAplication 的 SKSAplication 类

public class SKSApplication extends MobileApplication{
   private static SKSApplication instance;
   public static final String DIRECT_MESSAGE_VIEW = "DIRECT_MESSAGE_VIEW";
   public static final String GROUP_MESSAGE_VIEW = "GROUP_MESSAGE_VIEW";

   public SKSApplication() {
       instance = this;
   }

   public static SKSApplication getInstance() {
       return instance;
   }

   addViewFactory(HOME_VIEW, () -> {
        HomeView homeView = new HomeView();
        homePresenter = (HomePresenter) homeView.getPresenter();
        return (View) homeView.getView();
    });

    addViewFactory(DIRECT_MESSAGE_VIEW, () -> {
        DirectMessageView directMessageView = new DirectMessageView();
        return (View) directMessageView.getView();
    });

    addViewFactory(GROUP_MESSAGE_VIEW, () -> {
        GroupMessageView groupMessageView = new GroupMessageView();
        return (View) groupMessageView.getView();
    });

    public void doRefreshMessageUI(String messageContent,Integer senderId,String senderName, String comId ) {
       if (DIRECT_MESSAGE_VIEW.equals(getView().getName())) {

在这里,我尝试从 doRefreshMessageUI 方法中的 getView() 检索直接消息视图,但它不起作用

      DirectMessageView view = (DirectMessageView) getView(); 
          DirectMessagePresenter presenter = (DirectMessagePresenter) view.getPresenter();
presenter.displayIncomingMessage(messageContent,senderId,senderName,comId);
    }
}
 }

先感谢您

4

1 回答 1

0

创建视图后,您可以检索视图的演示者:

private DirectMessagePresenter presenter;

@Override
public void init() {
    addViewFactory(HOME_VIEW, () -> {
        HomeView homeView = new HomeView();
        homePresenter = (HomePresenter) homeView.getPresenter();
        return (View) homeView.getView();
    });

    addViewFactory(DIRECT_MESSAGE_VIEW, () -> {
        DirectMessageView directMessageView = new DirectMessageView();
        presenter = (DirectMessagePresenter) directMessageView.getPresenter();
        return (View) directMessageView.getView();
    });
}

无论您显示的是哪个当前视图,这都将起作用。

稍后,当您想要传递一些新值时,您可以使用您的方法:

public void doRefreshMessageUI(String messageContent,Integer senderId,String senderName, String comId ) {
   presenter.displayIncomingMessage(messageContent,senderId,senderName,comId);
}

假设在该演示者上您具有显示新值的控件,您只需在此处设置它们:

public void displayIncomingMessage(String messageContent, Integer senderId, String senderName, String comId){
    labelContent.setText(messageContent);
    labelSenderId.setText(senderId.toString());
    labelName.setText(senderName);
    labelComId.setText(comId);
}

同样,您可以使用 JavaFX 属性:

private final StringProperty content = new SimpleStringProperty();
private final IntegerProperty senderId = new SimpleIntegerProperty();
private final StringProperty name = new SimpleStringProperty();
private final StringProperty comId = new SimpleStringProperty();

@Override
public void initialize(URL url, ResourceBundle rb) {
    labelContent.textProperty().bind(content);
    labelSenderId.textProperty().bind(senderId.asString());
    labelName.textProperty().bind(name);
    labelComId.textProperty().bind(comId);
}

public void displayIncomingMessage(String messageContent, Integer senderId, String senderName, String comId){
    this.content.set(messageContent);
    this.senderId.set(senderId);
    this.name.set(senderName);
    this.comId.set(comId);
}
于 2016-01-28T11:56:51.353 回答