跟据上述电商app案例给出完整代码
时间: 2023-08-16 15:03:29 浏览: 42
以下是一个简单的电商App的完整代码示例。这个App展示了一个产品列表,当用户点击其中一个产品时,会跳转到该产品的详细信息页面。
MainActivity.java
```java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<Product> products = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化产品列表
initProducts();
// 设置产品列表的适配器
ProductAdapter adapter = new ProductAdapter(this, products);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(adapter);
// 设置产品列表的点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
// 跳转到产品详情页
Intent intent = new Intent(MainActivity.this, ProductDetailActivity.class);
intent.putExtra("product", products.get(position));
startActivity(intent);
}
});
}
// 初始化产品列表
private void initProducts() {
products.add(new Product("商品1", "这是商品1的描述", 50));
products.add(new Product("商品2", "这是商品2的描述", 100));
products.add(new Product("商品3", "这是商品3的描述", 150));
products.add(new Product("商品4", "这是商品4的描述", 200));
products.add(new Product("商品5", "这是商品5的描述", 250));
products.add(new Product("商品6", "这是商品6的描述", 300));
}
}
```
ProductAdapter.java
```java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class ProductAdapter extends BaseAdapter {
private Context context;
private ArrayList<Product> products;
public ProductAdapter(Context context, ArrayList<Product> products) {
this.context = context;
this.products = products;
}
@Override
public int getCount() {
return products.size();
}
@Override
public Object getItem(int position) {
return products.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}
TextView nameTextView = convertView.findViewById(R.id.name_text_view);
TextView priceTextView = convertView.findViewById(R.id.price_text_view);
Product product = products.get(position);
nameTextView.setText(product.getName());
priceTextView.setText(String.valueOf(product.getPrice()));
return convertView;
}
}
```
ProductDetailActivity.java
```java
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class ProductDetailActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_detail);
// 获取传递过来的产品对象
Product product = (Product) getIntent().getSerializableExtra("product");
// 显示产品的详细信息
ImageView imageView = findViewById(R.id.image_view);
TextView nameTextView = findViewById(R.id.name_text_view);
TextView descriptionTextView = findViewById(R.id.description_text_view);
TextView priceTextView = findViewById(R.id.price_text_view);
imageView.setImageResource(R.drawable.product_image);
nameTextView.setText(product.getName());
descriptionTextView.setText(product.getDescription());
priceTextView.setText(String.valueOf(product.getPrice()));
}
}
```
activity_main.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
```
list_item.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<TextView
android:id="@+id/name_text_view"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"/>
</LinearLayout>
```
activity_product_detail.xml
```xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/name_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:paddingTop="16dp"
android:paddingBottom="16dp"/>
<TextView
android:id="@+id/description_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:paddingBottom="16dp"/>
<TextView
android:id="@+id/price_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:paddingBottom="16dp"/>
</LinearLayout>
</ScrollView>
```
Product.java
```java
import java.io.Serializable;
public class Product implements Serializable {
private String name;
private String description;
private int price;
public Product(String name, String description, int price) {
this.name = name;
this.description = description;
this.price = price;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public int getPrice() {
return price;
}
}
```