Android 实现购物车

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11272007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:37:14  来源:igfitidea点击:

Implementing a Shopping Cart

androidsqliteandroid-activityglobal-variablesshopping-cart

提问by kb_14

I am making an Android app where I am implementing a Shopping Cart. When the user selects an item, it is added to his/her shopping cart/bag in the app. Once he/she adds an item, he's presented with the menu again, where he can add more items. Now I want to know how do I save these items during the buying process. I mean the items that are being saved in the bag, how do I save them in my android app? Shall I use the Android SQLite database? or use putextra/getextra and navigate through activities? or shall I use Global Variables (something like subclassing the Application class)?

我正在制作一个 Android 应用程序,我将在其中实施购物车。当用户选择一个项目时,它会被添加到他/她在应用程序中的购物车/包中。一旦他/她添加了一个项目,他就会再次看到菜单,在那里他可以添加更多项目。现在我想知道如何在购买过程中保存这些物品。我的意思是保存在包中的物品,我如何将它们保存在我的 android 应用程序中?我应该使用 Android SQLite 数据库吗?或使用 putextra/getextra 并浏览活动?还是我应该使用全局变量(类似于对 Application 类进行子类化)?

Or is there any other better way to implement such a thing? The details that I need to show in my bag are: the name of the product, its price, quantity and a few other attributes/options related to the product.

或者有没有其他更好的方法来实现这样的事情?我需要在包中显示的详细信息是:产品名称、价格、数量以及与产品相关的其他一些属性/选项。

回答by dymmeh

Passing the shopping cart items around in extras or using an Application class is too volatile. If your app crashes, the user exits the app, android decides to close your app, .. basically anything that closes your app.. The user will lose the contents of their shopping cart.

在额外内容中传递购物车项目或使用 Application 类太不稳定了。如果您的应用程序崩溃,用户退出该应用程序,android 决定关闭您的应用程序,.. 基本上任何关闭您的应用程序.. 用户将丢失其购物车中的内容。

SQLite would be a good option for storing data like this. You could also store the shopping cart in an xml file on the user's phone.

SQLite 将是存储此类数据的不错选择。您还可以将购物车存储在用户手机上的 xml 文件中。

回答by adatapost

Is there any other better way to implement such a thing?

有没有其他更好的方法来实现这样的事情?

First of all you have to understand that what isShopping Cart Software? However you may develop shopping cart client(Tutorials) in android.

首先你要明白什么购物车软件?但是,您可以在 android 中开发购物车客户端(教程)。

The shopping cart system has server-application (which you may develop using the RESTfulweb-services,servlets and filter) and client-application (you may develop a web-client or Android client). The RESTful web-services, which expose Shopping Cart APIand you may interact them (Web-services) with Android UI and network API (Apache http client).

购物车系统具有服务器应用程序(您可以使用RESTfulWeb 服务、servlet 和过滤器开发)和客户端应用程序(您可以开发 Web 客户端或 Android 客户端)。RESTful Web 服务,它公开Shopping Cart API并且您可以将它们(Web 服务)与 Android UI 和网络 API(Apache http 客户端)进行交互。

回答by Tony Vu

One way is to have your shopping cart stored in a public static variables. Since I had to implement the shopping cart for few of my Android apps, I have tried to create a library project for it.

一种方法是将您的购物车存储在公共静态变量中。由于我必须为我的一些 Android 应用程序实现购物车,因此我尝试为它创建一个库项目。

You can check it out here: https://github.com/tonyvu2014/android-shoppingcartand an example on how to use the library is here: https://github.com/tonyvu2014/android-shoppingcart-demo

您可以在此处查看:https: //github.com/tonyvu2014/android-shoppingcart,有关如何使用该库的示例在此处:https: //github.com/tonyvu2014/android-shoppingcart-demo