可以用android studio代码写出来吗
时间: 2023-08-10 15:49:31 浏览: 137
当然可以,以下是一些示例代码:
1. 产品介绍和选购功能页面的实现代码:
```
public class ProductActivity extends AppCompatActivity implements ProductAdapter.OnItemClickListener {
private RecyclerView mRecyclerView;
private ProductAdapter mAdapter;
private List<Product> mProductList;
private SQLiteDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product);
// 初始化RecyclerView
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// 初始化数据库
DBHelper dbHelper = new DBHelper(this);
mDatabase = dbHelper.getWritableDatabase();
// 加载商品数据
mProductList = new ArrayList<>();
Cursor cursor = mDatabase.query("product", null, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
String description = cursor.getString(cursor.getColumnIndex("description"));
String image = cursor.getString(cursor.getColumnIndex("image"));
Product product = new Product(id, name, price, description, image);
mProductList.add(product);
}
cursor.close();
// 初始化适配器
mAdapter = new ProductAdapter(mProductList);
mAdapter.setOnItemClickListener(this);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onItemClick(int position) {
// 处理商品选购事件,将选购信息保存到数据库
Product product = mProductList.get(position);
ContentValues values = new ContentValues();
values.put("name", product.getName());
values.put("price", product.getPrice());
values.put("quantity", 1);
values.put("subtotal", product.getPrice());
mDatabase.insert("cart", null, values);
Toast.makeText(this, "已加入购物车", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
mDatabase.close();
}
}
```
2. 购物车展示功能页面的实现代码:
```
public class CartActivity extends AppCompatActivity implements CartAdapter.OnItemClickListener {
private RecyclerView mRecyclerView;
private CartAdapter mAdapter;
private List<CartItem> mCartList;
private SQLiteDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
// 初始化RecyclerView
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// 初始化数据库
DBHelper dbHelper = new DBHelper(this);
mDatabase = dbHelper.getWritableDatabase();
// 加载购物车数据
mCartList = new ArrayList<>();
Cursor cursor = mDatabase.query("cart", null, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
int quantity = cursor.getInt(cursor.getColumnIndex("quantity"));
double subtotal = cursor.getDouble(cursor.getColumnIndex("subtotal"));
CartItem item = new CartItem(id, name, price, quantity, subtotal);
mCartList.add(item);
}
cursor.close();
// 初始化适配器
mAdapter = new CartAdapter(mCartList);
mAdapter.setOnItemClickListener(this);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onItemClick(int position, int quantity) {
// 处理修改购物车商品数量事件,更新数据库
CartItem item = mCartList.get(position);
item.setQuantity(quantity);
item.setSubtotal(item.getPrice() * quantity);
ContentValues values = new ContentValues();
values.put("quantity", quantity);
values.put("subtotal", item.getSubtotal());
mDatabase.update("cart", values, "id = ?", new String[] {String.valueOf(item.getId())});
mAdapter.notifyItemChanged(position);
}
@Override
public void onDeleteClick(int position) {
// 处理删除购物车商品事件,从数据库中删除
CartItem item = mCartList.get(position);
mDatabase.delete("cart", "id = ?", new String[] {String.valueOf(item.getId())});
mCartList.remove(position);
mAdapter.notifyItemRemoved(position);
}
@Override
protected void onDestroy() {
super.onDestroy();
mDatabase.close();
}
}
```
3. 外网链接功能的实现代码:
```
public class MainActivity extends AppCompatActivity {
private Button mLinkButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化外网链接按钮
mLinkButton = findViewById(R.id.link_button);
mLinkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到指定网址
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.example.com"));
startActivity(intent);
}
});
}
}
```
阅读全文