android studio实现简单购物车项目完整代码
时间: 2023-10-21 07:02:46 浏览: 310
以下是一个简单的购物车项目的完整代码,包含了商品列表、购物车功能和结算功能。
MainActivity.java
```
package com.example.shoppingcart;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private TextView totalTextView;
private Button checkoutButton;
private ArrayList<Product> productList;
private ArrayAdapter<Product> productAdapter;
private ArrayList<Product> cartList;
private ArrayAdapter<Product> cartAdapter;
private double total = 0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
totalTextView = findViewById(R.id.totalTextView);
checkoutButton = findViewById(R.id.checkoutButton);
productList = new ArrayList<Product>();
productList.add(new Product("Product 1", 10.0));
productList.add(new Product("Product 2", 20.0));
productList.add(new Product("Product 3", 30.0));
productList.add(new Product("Product 4", 40.0));
productList.add(new Product("Product 5", 50.0));
productAdapter = new ArrayAdapter<Product>(this, android.R.layout.simple_list_item_1, productList);
listView.setAdapter(productAdapter);
cartList = new ArrayList<Product>();
cartAdapter = new ArrayAdapter<Product>(this, android.R.layout.simple_list_item_1, cartList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Product product = productAdapter.getItem(i);
cartList.add(product);
cartAdapter.notifyDataSetChanged();
total += product.getPrice();
totalTextView.setText("Total: $" + total);
}
});
checkoutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (cartList.isEmpty()) {
Toast.makeText(MainActivity.this, "Your cart is empty", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Thank you for your purchase!", Toast.LENGTH_SHORT).show();
cartList.clear();
cartAdapter.notifyDataSetChanged();
total = 0.0;
totalTextView.setText("Total: $" + total);
}
}
});
}
}
```
Product.java
```
package com.example.shoppingcart;
public class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return name + " ($" + price + ")";
}
}
```
activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/totalTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Total: $0.0"
android:textSize="18sp" />
<Button
android:id="@+id/checkoutButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checkout"
android:textSize="18sp" />
</LinearLayout>
<ListView
android:id="@+id/cartListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
```
这个购物车项目使用了两个ListView,一个显示商品列表,一个显示购物车列表。当用户点击商品列表中的某个商品时,该商品将会被添加到购物车中,并更新购物车列表和总价。当用户点击结算按钮时,如果购物车为空,将会弹出提示信息;否则,会弹出感谢购买的提示信息,并清空购物车列表和总价。
阅读全文