接上文
ShoppingChannelActivity
package com.example.shop;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.shop.database.ShoppingDBHelper;
import com.example.shop.enity.GoodsInfo;
import java.util.List;
public class ShoppingChannelActivity extends AppCompatActivity implements View.OnClickListener {
private ShoppingDBHelper mDBHelper;
public TextView tv_count;
public GridLayout gl_Channel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_channel);
TextView textView=findViewById(R.id.tv_title);
textView.setText("mobel phone shop");
tv_count = findViewById(R.id.tv_count);
gl_Channel=findViewById(R.id.gl_channel);
findViewById(R.id.iv_back).setOnClickListener(this);
findViewById(R.id.iv_cart).setOnClickListener(this);
mDBHelper=ShoppingDBHelper.getInstance(this);
mDBHelper.openReadLink();
mDBHelper.openWriteLink();
showGoods();
}
protected void onResume(){//购物车初始化
super.onResume();
showCartInfoTotal();
}
private void showCartInfoTotal() {//查询商品总数
// int count=mDBHelper.countCartInfo();
// MyApplication.getInstance().goodsCount=count;
// tv_count.setText(String.valueOf(count));
tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));
}
private void showGoods(){
int screenWidth=getResources().getDisplayMetrics().widthPixels;//设置宽高
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(screenWidth/2, ViewGroup.LayoutParams.WRAP_CONTENT);
List<GoodsInfo>list=mDBHelper.queryAllGoodsInfo();
gl_Channel.removeAllViews();//清空
for(int i=0;i<6;i++){
GoodsInfo info= list.get(i);
View view=LayoutInflater.from(this).inflate(R.layout.item_goods,null);
// @SuppressLint({"MissingInflatedId", "LocalSuppress"})
ImageView iv_thumb=view.findViewById(R.id.iv_thumb);
// @SuppressLint({"MissingInflatedId", "LocalSuppress"})
TextView tv_name=view.findViewById(R.id.tv_name);
// @SuppressLint({"MissingInflatedId", "LocalSuppress"})
TextView tv_price=view.findViewById(R.id.tv_price);
Button btn_add=view.findViewById(R.id.btn_add);//添加购物车
iv_thumb.setImageURI(Uri.parse(info.picPath));
tv_name.setText(info.name);
tv_price.setText(String.valueOf((int)info.price));
btn_add.setOnClickListener(v->{
addToCart(info.id,info.name);
});
//点击商品图片跳转介绍
iv_thumb.setOnClickListener(v -> {
Intent intent=new Intent(ShoppingChannelActivity.this, ShoppingDetailActivity.class);
intent.putExtra("goods_id",info.id);
startActivity(intent);
});
//设置宽高
gl_Channel.addView(view,params);
}
}
private void addToCart(int Gid, String name) {
int count=++MyApplication.getInstance().goodsCount;
tv_count.setText(String.valueOf(count));
mDBHelper.insertCartInfo(Gid);
Toast.makeText(this,"已添加"+Gid+"到购物车",Toast.LENGTH_SHORT).show();
}
protected void onDestory(){
super.onDestroy();
mDBHelper.closeLink();
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.iv_back){
finish();
}
else{
Intent intent=new Intent(this,ShoppingcartActivit.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
}
ShoppingDetailActivity
package com.example.shop;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.shop.database.ShoppingDBHelper;
import com.example.shop.enity.GoodsInfo;
public class ShoppingDetailActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_title;
private TextView tv_count;
private TextView tv_goods_price;
private TextView tv_goods_des;
private ImageView iv_goods_pic;
private ShoppingDBHelper mdbHelper;
private int mgoodsId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shopping_detail);
tv_title=findViewById(R.id.tv_title);
tv_count=findViewById(R.id.tv_count);
tv_goods_des=findViewById(R.id.tv_goods_des);
tv_goods_price=findViewById(R.id.tv_goods_price);
iv_goods_pic=findViewById(R.id.iv_goods_pic);
tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));
findViewById(R.id.iv_back).setOnClickListener(this);
findViewById(R.id.iv_cart).setOnClickListener(this);
findViewById(R.id.btn_add_cart).setOnClickListener(this);
mdbHelper=ShoppingDBHelper.getInstance(this);
}
protected void onResume() {
super.onResume();
showDetail();
}
private void showDetail() {
mgoodsId=getIntent().getIntExtra("goods_id",0);
if(mgoodsId>0){//查询
GoodsInfo info=mdbHelper.queryGoodsInfoById(mgoodsId);
tv_title.setText(info.name);
tv_goods_des.setText(info.description);
tv_goods_price.setText(String.valueOf((int)info.price));
iv_goods_pic.setImageURI(Uri.parse(info.picPath));
}
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.iv_back){
finish();
} else if (v.getId()==R.id.iv_cart) {//跳转
Intent intent=new Intent(this,ShoppingcartActivit.class);
startActivity(intent);
}else{//加入购物车
addToCart(mgoodsId);
}
}
private void addToCart(int goodsId) {
int count=++MyApplication.getInstance().goodsCount;
tv_count.setText(String.valueOf(count));
mdbHelper.insertCartInfo(goodsId);
Toast.makeText(this,"已添加到购物车",Toast.LENGTH_SHORT).show();
tv_count.setText(String.valueOf(MyApplication.getInstance().goodsCount));
}
}
下面是xml
/res/drawable/shape_oval_red.xml 单纯画个圆形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#ffA266"/>
</shape>
/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
/res/layout/activity_shopping_channel.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">
<include layout="@layout/title_shopping"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<GridLayout
android:id="@+id/gl_channel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"/>
</ScrollView>
</LinearLayout>
/res/layout/activity_shopping_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/title_shopping"></include>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/iv_goods_pic"
android:layout_width="match_parent"
android:layout_height="320dp"
android:scaleType="fitCenter"
tools:src="@drawable/no1"/>
<TextView
android:id="@+id/tv_goods_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="20sp"
tools:text="200"/>
<TextView
android:id="@+id/tv_goods_des"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textSize="20sp"
tools:text="xiaomi 256G 1222"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_add_cart"
android:text="加入购物车"
android:textSize="17sp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
/res/layout/activity_shoppingcart.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/title_shopping"></include>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_content"
android:orientation="vertical"
android:visibility="visible">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="85dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/picture"
android:textSize="15sp"
android:textColor="@color/black"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="@string/name"
android:textSize="15sp"
android:textColor="@color/black"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/count"
android:textSize="15sp"
android:textColor="@color/black"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/price"
android:textSize="15sp"
android:textColor="@color/black"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="@string/sum_price"
android:textSize="15sp"
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_cart"
android:orientation="vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="0dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_clear"
android:gravity="center"
android:text="@string/clear"
android:textColor="@color/black"
android:textSize="17sp"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center|right"
android:layout_weight="1"
android:text="@string/sum"
android:textColor="@color/black"
android:textSize="17sp"
tools:ignore="RtlHardcoded" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_total_price"
android:gravity="center|left"
android:textColor="@color/black"
android:textSize="25sp"
tools:ignore="RtlHardcoded" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_settle"
android:gravity="center"
android:text="@string/jiesuan"
android:textColor="@color/black"
android:textSize="17sp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_empty"
android:visibility="gone"
android:orientation="vertical"
tools:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:layout_marginBottom="100dp"
android:gravity="center"
android:text="@string/kong"
tools:visibility="visible"
android:textColor="@color/black"
android:textSize="17sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_shopping_channel"
android:gravity="center"
tools:visibility="visible"
android:text="@string/guang"
android:textColor="@color/black"
android:textSize="17sp"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
/res/layout/item_cart.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="85dp"
android:layout_height="85dp"
android:id="@+id/iv_thumb"
android:scaleType="fitCenter"
tools:src="@drawable/no1"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/tv_name"
android:layout_weight="2"
android:gravity="left|center"
android:textSize="17sp"
tools:text="xiaomi"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="@+id/tv_desc"
android:layout_weight="3"
android:gravity="left|center"
android:textSize="12sp"
tools:text="xiaomi 256G phone"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/tv_count"
android:layout_weight="1"
android:gravity="center"
android:textSize="17sp"
tools:text="2"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/tv_price"
android:layout_weight="1"
android:gravity="center"
android:textSize="17sp"
tools:text="200"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/tv_sum"
android:layout_weight="1"
android:gravity="center"
android:textSize="19sp"
tools:text="400"/>
</LinearLayout>
/res/layout/item_goods.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/white"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_name"
android:gravity="center"
android:textColor="@color/black"
android:textSize="18sp"
tools:text="xiaomi"/>
<ImageView
android:id="@+id/iv_thumb"
android:layout_width="180dp"
android:layout_height="150dp"
android:scaleType="fitCenter"
tools:src="@drawable/no1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/tv_price"
android:layout_weight="2"
android:gravity="center"
android:textColor="@color/black"
android:textSize="15sp"
tools:text="200"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="@+id/btn_add"
android:layout_weight="3"
android:text="加入购物车"/>
</LinearLayout>
</LinearLayout>
/res/layout/title_shopping.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:id="@+id/iv_back"
android:padding="10dp"
android:layout_alignParentLeft="true"
android:scaleType="fitCenter"
android:src="@drawable/ic_cart"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/horizon_blue"
android:layout_centerInParent="true"
android:gravity="center"
android:textSize="20dp" />
<ImageView
android:layout_width="50dp"
android:layout_height="match_parent"
android:id="@+id/iv_cart"
android:padding="10dp"
android:layout_alignParentRight="true"
android:scaleType="fitCenter"
android:src="@drawable/ic_back"/>
<TextView
android:id="@+id/tv_count"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/iv_cart"
android:layout_marginLeft="-20dp"
android:background="@drawable/shape_oval_red"
android:gravity="center"
android:text="0"
android:textColor="@color/white"
android:textSize="15dp" />
</RelativeLayout>