zoukankan      html  css  js  c++  java
  • Android-9

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:app="http://schemas.android.com/apk/res-auto"
     4     xmlns:tools="http://schemas.android.com/tools"
     5     android:layout_width="match_parent"
     6     android:layout_height="match_parent"
     7     android:background="#FF5722"
     8     android:orientation="vertical"
     9     tools:context=".MainActivity">
    10 
    11     <TextView
    12         android:id="@+id/textView"
    13         android:layout_width="match_parent"
    14         android:layout_height="40dp"
    15         android:background="#FF5722"
    16         android:gravity="center"
    17         android:text="购物车"
    18         android:textSize="24sp"
    19         android:textStyle="bold" />
    20 
    21     <LinearLayout
    22         android:layout_width="match_parent"
    23         android:layout_height="match_parent"
    24         android:layout_weight="1"
    25         android:orientation="vertical">
    26 
    27         <ListView
    28             android:id="@+id/buylist"
    29             android:layout_width="match_parent"
    30             android:layout_height="match_parent"
    31             android:background="#F1F0EF" />
    32 
    33     </LinearLayout>
    34 
    35     <Button
    36         android:id="@+id/but_sum"
    37         android:layout_width="match_parent"
    38         android:layout_height="wrap_content"
    39         android:background="#FF5722"
    40         android:text="结算"
    41         android:textSize="18sp"
    42         app:backgroundTint="#FF5722" />
    43 
    44 </LinearLayout>
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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="120dp"
        android:layout_marginTop="10px"
        android:layout_marginBottom="20px"
        android:background="#F6F2F0"
        android:gravity="center">
    
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            tools:srcCompat="@tools:sample/backgrounds/scenic" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/t_topic"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="120px"
                android:text="苹果"
                android:textColor="@color/black" />
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
    
                <TextView
                    android:id="@+id/t_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="120px"
                    android:gravity="right"
                    android:text="六"
                    android:textColor="@color/black" />
    
                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="元1斤"
                    android:textColor="@color/black" />
            </LinearLayout>
    
            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center">
    
                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="30dp"
                    android:gravity="center">
    
                    <TextView
                        android:id="@+id/textView4"
                        android:layout_width="150dp"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:text="购买数量"
                        android:textColor="@color/black" />
    
                    <Button
                        android:id="@+id/add"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:background="@mipmap/add"
                        android:gravity="center"
                        android:textColor="@color/black"
                        android:textSize="14sp"
                        app:backgroundTint="#FF5722" />
    
                    <TextView
                        android:id="@+id/t_view"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:gravity="center"
                        android:text="0"
                        android:textColor="@color/black" />
    
                    <Button
                        android:id="@+id/reduce"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:autoText="false"
                        android:background="@mipmap/reduce"
                        android:freezesText="false"
                        android:gravity="center"
                        android:textColor="@color/black"
                        android:textSize="14sp"
                        app:backgroundTint="#FF5722" />
                </TableRow>
    
            </TableLayout>
        </LinearLayout>
    </LinearLayout>
    <?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:background="#FFFFFF"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#FF5722"
            android:gravity="center"
            android:text="结算"
            android:textColor="@color/black"
            android:textSize="24sp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">
    
            <ListView
                android:id="@+id/sumlist"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#F1F0EF" />
    
        </LinearLayout>
    
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="总计" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="修改" />
    
    </LinearLayout>
    <?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:background="#FFFFFF">
    
        <TextView
            android:id="@+id/s_name"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:textColor="@color/black" />
    
        <TextView
            android:id="@+id/s_price"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_weight="1"
            android:textColor="@color/black" />
    </LinearLayout>
    package com.example.student;
    
    /*实体类*/
    public class Buy {
    
        private int imageId;
        private String Name,price,sum;
    
    
        public Buy(int imageId, String name, String price) {
            this.imageId = imageId;
            Name = name;
            this.price = price;
        }
    
        public Buy(String name, String sum) {
            Name = name;
            this.sum = sum;
        }
    
        public int getImageId() {
            return imageId;
        }
    
        public void setImageId(int imageId) {
            this.imageId = imageId;
        }
    
        public String getName() {
            return Name;
        }
    
        public void setName(String name) {
            Name = name;
        }
    
        public String getPrice() {
            return price;
        }
    
        public void setPrice(String price) {
            this.price = price+"元1斤";
        }
    
        public String getSum() {
            return sum;
        }
    
        public void setSum(String sum) {
            this.sum = sum;
        }
    
        public Object getItem(int position){
            return position;
        }
    
    }
    package com.example.student;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    import androidx.annotation.Nullable;
    /*SQL类*/
    public class BuySql extends SQLiteOpenHelper {
        public BuySql(@Nullable Context context) {
            super(context, "buy.db", null, 2);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE buy(_id INTEGER PRIMARY KEY AUTOINCREMENT,b_name VARCHAR(20),b_price VARCHAR(30),b_sum VARCHAR(30))");
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
        }
    }
    package com.example.student;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends AppCompatActivity  {
    
    
        final List<Buy> buysList = new ArrayList<Buy>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initFruits();
            BuyAdapte adapter = new BuyAdapte(this,R.layout.item,buysList);
            ListView lv = (ListView)findViewById(R.id.buylist);
            lv.setAdapter(adapter);
            Button but_sum = (Button)findViewById(R.id.but_sum);
    
            but_sum.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this,SumActivity.class);
    
                    startActivityForResult(intent,1);
                }
            });
        }
    
    
    
    
        /*数据输入方法,调用实体类*/
        public void initFruits(){
            Buy apple = new Buy(R.mipmap.apple,"苹果","5");//sum是购物车中添加的物品多少
            buysList.add(apple);
            Buy banana = new Buy(R.mipmap.banana,"香蕉","6.5");
            buysList.add(banana);
            Buy grape = new Buy(R.mipmap.grape,"葡萄","10");
            buysList.add(grape);
            Buy kiwi = new Buy(R.mipmap.kiwi,"猕猴桃","12");
            buysList.add(kiwi);
            Buy mango = new Buy(R.mipmap.mango,"芒果","6");
            buysList.add(mango);
            Buy orange = new Buy(R.mipmap.orange,"橘子","4.8");
            buysList.add(orange);
    
        }
    
    
    
        /*数据适配器Adapte*/
    
        public class BuyAdapte extends ArrayAdapter {
            int resourceId;
            public BuyAdapte(@NonNull Context context, int resource, List<Buy> objects) {
                super(context, resource,objects);
                resourceId = resource;
            }
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                //获取当前Buy实例
                Buy buy = (Buy) getItem(position);
                View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
                ImageView iv = (ImageView) view.findViewById(R.id.imageView);
                TextView tv = (TextView) view.findViewById(R.id.t_topic);
                TextView pv = (TextView) view.findViewById(R.id.t_price);
                TextView ta = (TextView) view.findViewById(R.id.t_view);
                Button add = (Button)view.findViewById(R.id.add);
                Button reduce = (Button)view.findViewById(R.id.reduce);
                iv.setImageResource(buy.getImageId());
                tv.setText((CharSequence) buy.getName());
                pv.setText((CharSequence)buy.getPrice());
                add.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int n1 = Integer.parseInt(ta.getText().toString());
                        if(n1>=0){
                            n1 = n1 + 1;
                            String a = String.valueOf(n1);
                            ta.setText(a);
                            buy.setSum(a);
                        }
                    }
                });
                reduce.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int n1=Integer.parseInt(ta.getText().toString());
                        if (n1>0){
                            n1=n1-1;
                            String a = String.valueOf(n1);
                            ta.setText(a);
                            buy.setSum(a);
                        }
                    }
                });
    
    
    
                return view;
            }
    
    
    
        }
    
    
    
    
    
    }
    package com.example.student;
    
    import androidx.annotation.NonNull;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class SumActivity extends AppCompatActivity {
    
        final List<Buy> sumsList = new ArrayList<Buy>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sum2);
            buyFruits();
            SumActivity.SumAdapte adapter = new SumActivity.SumAdapte(this,R.layout.sum_item,sumsList);
            ListView lv = (ListView)findViewById(R.id.sumlist);
            lv.setAdapter(adapter);
    
            Button but = (Button)findViewById(R.id.button);
    
            but.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(SumActivity.this,MainActivity.class);
    
                    setResult(2,intent);
                    finish();
                }
            });
    
        }
    
    
        public void buyFruits(){
    
            Buy apple = new Buy("苹果","15");//sum是购物车中添加的物品多少
            sumsList.add(apple);
            Buy banana = new Buy("香蕉","6.5");
            sumsList.add(banana);
            Buy grape = new Buy("葡萄","10");
            sumsList.add(grape);
            Buy kiwi = new Buy("猕猴桃","12");
            sumsList.add(kiwi);
            Buy mango = new Buy("芒果","6");
            sumsList.add(mango);
            Buy orange = new Buy("橘子","4.8");
            sumsList.add(orange);
    
        }
    
    
    
    
        public class SumAdapte extends ArrayAdapter {
            int resourceId;
            public SumAdapte(@NonNull Context context, int resource, List<Buy> objects) {
                super(context, resource,objects);
                resourceId = resource;
            }
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                //获取当前Buy实例
                Buy buy = (Buy) getItem(position);
                View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
                TextView tv = (TextView) view.findViewById(R.id.s_name);
                TextView pv = (TextView) view.findViewById(R.id.s_price);
                tv.setText( buy.getName());
                pv.setText(buy.getSum());
    
                return view;
            }
    
    
    
        }
    
    
    
    
    
    
    }

     

  • 相关阅读:
    jquery的$.与$.fn的区别
    jquery加载页面的方法
    创业股权究竟如何分配--新浪创业训练营
    创业者要处理好的10大关系
    洪泰基金投资经理殷鹏:肯定不投的八类项目
    创业初期股权如何分配-------陈楠心血总结
    排序总结
    如何快速进入一门领域,学习新的知识
    虚拟机的设置
    华为大数据项目fusionInsight
  • 原文地址:https://www.cnblogs.com/yunlan/p/14018108.html
Copyright © 2011-2022 走看看