简体   繁体   English

如何将选定项目的ID从ListView传递到Android中的AlertDialog?

[英]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放入该捆绑包中,然后可以在onPrepareDialogonPrepareDialog进行访问。 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.

相关问题 如何从数据库的listView中的选定项中获取数据并在alertdialog上查看? - how I can get the data from the selected item in the listView from my database and view it on alertdialog? Android Alertdialog一旦我从单选项目中选择了一个项目,如何防止否定按钮确认操作 - Android alertdialog how do I prevent the negative button confirm the action once I selected an item from the single choice items 如何将从服务器获取的listview项的ID传递给另一个活动onclick? - How do I pass id of listview item I got from server to another activity onclick? 单击ListView项目时如何关闭AlertDialog - How do I close AlertDialog when I click on ListView item 如何在Android ListView中设置所选项目的样式? - How do I style selected item in Android ListView? 如何从列表视图传递选定的项目(字符串) - how to pass the selected item (string) from listview Android,如何从数据库更新列表视图中的选定项目? - Android, How do i Update the selected item in my listview from my database? Android Studio 将数据从 ListView 传递到 AlertDialog - Android Studio Pass data from a ListView to AlertDialog 我想获取所选商品的列表视图ID - i want to get listview's id of selected item 如何使用alertDialog从listView中删除项目 - How to delete item from listView using alertDialog
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM