android开发---简单购物商城(JAVA) (二)

接上文

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);

        Listlist=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  单纯画个圆形




/res/layout/activity_main.xml




    



    


/res/layout/activity_shopping_channel.xml



    
    
        
    

/res/layout/activity_shopping_detail.xml



    
    
        
            
            
            
            

/res/layout/activity_shoppingcart.xml



    
        
            
                
                    
                        
                        
                        
                        
                        

                    
                    
                    
                        

/res/layout/item_cart.xml



    
    
        
        


    

    
    
    

/res/layout/item_goods.xml



    
    
    
        
        

/res/layout/title_shopping.xml



    

    

    

    


你可能感兴趣的:(android,java,开发语言)