在Android应用程序中创建收藏夹按钮(需要传递对象而不改变Activity)
我正在开发我的第一个 Android 应用程序,需要一些帮助。我正在创建一个食谱页面,我想在上面有一个“收藏夹”按钮。我希望用户在查看菜谱时能够单击“收藏夹”按钮并将其添加到“收藏夹”页面。但是,当用户单击该按钮时,我希望他们能够继续查看该食谱页面,而不是移动到收藏夹页面。
我发现了如何在更改页面时使用意图将数据发送到另一个活动。但是我不希望页面发生变化。
我假设我会向活动传递一个按钮来执行此操作(有更好的方法吗?)
提前感谢您的所有帮助!
I am developing my first android app and need a little bit of help. I am creating a recipes page and I would like to have a Favorites button on it. I would like the user to be able to click the favorites button while they are viewing a recipe and have it added to the favorites page. However when the user clicks the button I would like them to be able to continue viewing that recipes page instead of moving to the favorites page.
I found out how to use intent to send data to another activity while changing the page. However I would not like the page to change.
I'm assuming I would be passing a button to the activity to do this(is there a better way?)
Thanks for all the help in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将所有收藏夹收集到一个列表中,并在用户最终想要切换页面时将它们一次性全部传递到下一个活动。或者当然,您可以简单地将列表设为收藏夹可以访问的公共静态成员,或者 - 我在这里的偏好 - 使用第三方保存所有收藏夹,该第三方也可以处理它的永久存储。这样,您还可以隔离一些依赖项:菜谱页面不会也不应该关心谁使用收藏夹数据以及如何使用。同样,我不认为收藏夹页面感兴趣的是何时按下收藏夹,而是当需要显示收藏夹时存在哪些收藏夹。
You could collect all the favorites to a list and pass them all at once to the next activity when the user finally wants to switch the page. Or of course, you could simply make the list a public static member which the favorites can access, or - my preference here - save all the favorites with a 3rd party which can also handle permanent storage of it. That way you also isolate some dependencies: the recipes page doesn't and shouldn't need to care about who uses the favorites data and how. Likewise, I don't assume the favorites page is interested in when a favorite is pressed, but what favorites exist when it needs to show them.
您只需将收藏夹列表保存在某个地方即可。在 SharedPreferences 中、在文件中或在数据库中。在按钮上单击将项目添加到列表中。您最喜欢的活动可以使用该列表:)
You just need to persist the favorites list somewhere. Either in SharedPreferences or in a file, or in a database. On the button click add the item to the list. Your favorites activity then can use the list :)