从对话框调用活动的方法
我有一个包含两个独立活动的选项卡界面,我们将它们称为 ActA 和 ActB。这两个活动都可以启动一个自定义对话框,我希望这个对话框中有一个按钮调用ActBs的方法,(ActB是数据库的ListView,所讨论的方法是填充列表,基本上是刷新) 。 我怎样才能让对话框活动引用 ActB 来调用它的方法?谢谢!
编辑:我会提供更多细节。我根据 Google 的记事本教程编写了这个程序,该教程教授有关 SQLite DB 的知识。我将其分为两个活动,一个用于创建条目,然后保存它们(ActA);另一个用于创建条目,然后保存它们(ActA);另一个用于查看条目(ActB)。现在,最近,我将这些活动放在选项卡中,而不是通过手机的菜单键访问它们。不幸的是,这似乎阻止了 ListActivity 调用其“fillData()”方法。
I have a Tab Interface with two separate activities, lets call them ActA and ActB. Both of these activities can launch a custom Dialog, and I would like to have a button within this dialog call a method of ActBs, (ActB is a ListView of a database, and the method in question is to fill the list, basically refresh).
How could I have the dialog box activity reference ActB to call its method? Thanks!
EDIT: I'll give some more details. I worked this program off of Google's Notepad tutorial which teaches about SQLite DBs. I took that and split it into two activities, one for creating the entries, and then saving them (ActA); and another for viewing the entries (ActB). Now, more recently, I put those activities in tabs rather than accessing them through the phone's menu key. Unfortunately, this appears to have stopped the ListActivity from calling its "fillData()" method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您要扩展 android Dialog 类,您可以实现 在您的活动中关闭监听器,并在创建对话框时设置它,然后在监听器中根据用于关闭对话框的按钮实现您想要的任何功能。
如果您的自定义对话框本身就是一个 Activity,并且您使用 startActivity 调用它,则可以将其更改为 startActivityForResult,并在刷新按钮中实现Dialog 的 setResult 具有自定义 Intent,通知调用 Activity 刷新自身。
有关如何使用 startActivityForResult 的更多信息,请查看此处。
If you are extending android Dialog class you can implement a Dismiss Listener in your Activity's and set it when you create the Dialog, and then in the listener implement any functionality you want depending on the button that was used to dismiss the Dialog.
If your custom Dialog is an Activity itself and you call it with a startActivity you can change it for a startActivityForResult, and implement in the refresh button of the Dialog a setResult with a custom Intent to inform the calling Activity to refresh itself.
For more info on how to use startActivityForResult look here.
A) 一种方法是使用广播和 BroadcastReceiver:http://developer.android。 com/reference/android/content/BroadcastReceiver.html
您为您的活动定义某些意图过滤器,然后通过以下方式发送广播
sendBroadcast(intent),其中意图触发活动中的方法。
B) 另一种方式:定义两个活动都实现的接口,即handleDialogButton。由于您在对话框构造函数中传递了上下文,因此您可以对它们执行一些操作。
A) One way is to use Broadcasts and BroadcastReceiver: http://developer.android.com/reference/android/content/BroadcastReceiver.html
You define certain intent filters for your activities, then send the broadcast via
sendBroadcast(intent), where the intent triggers the method in your activities.
B) Another way: define an interface that both activities are implementing, i.e. handleDialogButton. Since you pass the context in the dialogs constructor, you can execute some action on them.
尝试使用此代码通过单击对话框按钮启动新活动。
Try this code to start new activity from Dialog Button click.