[英]How do I pass the selected item's ID from a ListView to an AlertDialog in Android?
Here's the scenario: I have a ListActivity, and long-pressing on an item brings up a context menu. 这里是场景:我有一个ListActivity,长按一个项目会弹出一个上下文菜单。 One item in the context menu is "delete", and that brings up a confirmation box (and AlertDialog). 上下文菜单中的一项是“删除”,它会弹出一个确认框(和AlertDialog)。 When the user presses OK in the confirmation dialog, I need to know the ID of the item that was originally selected, so that I can actually delete it. 当用户在确认对话框中按“确定”时,我需要知道最初选择的项目的ID,以便实际上可以将其删除。
The flow looks like this: 流程如下所示:
This event: Causes Android to call: ----------------------------------------------------- Long press an item -> onCreateContextMenu() Select context menu item -> onContextItemSelected() call showDialog() -> onPrepareDialog() user clicks OK -> onClick()
In onCreateContextMenu and onContextMenuSelected, I can get at the id of the selected item from the ContextMenuInfo. 在onCreateContextMenu和onContextMenuSelected中,我可以从ContextMenuInfo中获得所选项目的ID。 In onPrepareDialog, however, I no longer have access to that information. 但是,在onPrepareDialog中,我不再有权访问该信息。 The rub is that onPrepareDialog needs this information to set up an onClick listener on its POSITIVE button. 问题是onPrepareDialog需要此信息来在其POSITIVE按钮上设置onClick侦听器。
I know that, during onContextMenuSelected, I can stash the selected item's ID away into a field of my activity. 我知道,在onContextMenuSelected期间,我可以将所选项目的ID保存到我的活动字段中。 I have done that, and it works. 我已经做到了,并且有效。 But it's also really ugly. 但这也真的很丑。 The statefulness that it introduces makes me uneasy. 它带来的有状态使我不安。 Has anybody else seen a better way to pass such information around than to use fields in the activity? 有没有其他人比使用活动中的字段看到更好的方法来传递此类信息?
Since you only have one user who can only make one long keypress at a time in your application (if I understood the documentation right), why would storing the id in a field of your own be bad? 由于您只有一个用户,一次只能在您的应用程序中进行一次长时间按键操作(如果我理解正确的文档说明),为什么将id存储在您自己的字段中会很糟糕? Sounds like a perfectly reasonable way to do it. 听起来是一种完全合理的方法。
You might want to consider storing the information in Preferences. 您可能要考虑将信息存储在首选项中。 They are a good way to store more persistent information. 它们是存储更多持久性信息的好方法。 By checking what in the preferences on your onCreate/onResume/onRestart you can figure out where to resume your application. 通过检查onCreate / onResume / onRestart上的首选项中的内容,您可以确定在哪里恢复应用程序。
http://developer.android.com/guide/topics/data/data-storage.html http://developer.android.com/guide/topics/data/data-storage.html
I see that, in Android 2.2 (API level 8), they added an overload to showDialog
that takes a bundle. 我看到,在Android 2.2(API级别8)中,他们向showDialog
添加了一个重载,该重载需要一个捆绑包。 It looks like you could use put the selected item's ID in that bundle, which is then accessible in onPrepareDialog
. 看起来您可以使用将所选项目的ID放入该捆绑包中,然后可以在onPrepareDialog
中onPrepareDialog
进行访问。 This is exactly what I was looking for. 这正是我想要的。 Unfortunately, my device is stuck on 1.6 (API level 4). 不幸的是,我的设备卡在1.6(API级别4)上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.