简体   繁体   English

如何从不同的类调用方法?

[英]How to call method from different class?

I have 2 classes, Main and DialogOne (an owner class for a dialog ui). 我有2个类, MainDialogOne (对话框ui的所有者类)。 In Main , I call new DialogOne().center(); Main ,我调用new DialogOne().center(); This brings up the dialog box. 这将弹出对话框。 In DialogOne , I have a clickhandler for a button. DialogOne ,我有一个按钮的clickhandler。 When the button is clicked, I insert a record into a database. 单击按钮后,我将一条记录插入数据库。

Main main = new Main(); //because the db.open is in Main

@UiHandler("addBookButton")
void onAddBookButtonClick(ClickEvent event) {
   main.db.transaction(new TransactionCallback() {
      public void onTransactionStart(SQLTransaction tx) {
         tx.executeSql("INSERT INTO products (bookName) VALUES (?)", new Object[] { bookNameTextBox.getText().toString() }); 
      }
      public void onTransactionFailure(SQLError error) {
      }
      public void onTransactionSuccess() {
      }
   });
this.hide();
}

In onTransactionSuccess() , I need to call updateList() which is in Main , so that the list is cleared and re-populated from the database. onTransactionSuccess() ,我需要调用Main updateList() ,以便从数据库中清除并重新填充列表。 How would I go about doing that? 我将如何去做? I tried main.updateList() but it didn't seem to work. 我尝试了main.updateList()但是似乎没有用。 Can I do something in Main that waits for onTransactionSuccess() to be hit and then updateList() 我可以在Main中做一些事情,等待onTransactionSuccess()被击中,然后等待updateList()

For this to work, the JVM would have to remember the context in which the interface was created, so that the reference to main doesn't ever get erased. 为此,JVM必须记住创建接口的上下文,以便永远不会删除对main的引用。 Java doesn't support that, except when the fields are final. Java不支持该功能,除非字段为final。 Have a look at the "Anonymous inner class it not a closure" part of this 看一看“匿名内部类是不封闭”的一部分,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM