在Android应用程序中创建收藏夹按钮(需要传递对象而不改变Activity)

发布于 2024-09-15 16:04:21 字数 253 浏览 2 评论 0原文

我正在开发我的第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅刃 2024-09-22 16:04:21

您可以将所有收藏夹收集到一个列表中,并在用户最终想要切换页面时将它们一次性全部传递到下一个活动。或者当然,您可以简单地将列表设为收藏夹可以访问的公共静态成员,或者 - 我在这里的偏好 - 使用第三方保存所有收藏夹,该第三方也可以处理它的永久存储。这样,您还可以隔离一些依赖项:菜谱页面不会也不应该关心谁使用收藏夹数据以及如何使用。同样,我不认为收藏夹页面感兴趣的是何时按下收藏夹,而是当需要显示收藏夹时存在哪些收藏夹。

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.

手长情犹 2024-09-22 16:04:21

您只需将收藏夹列表保存在某个地方即可。在 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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文