购物车

购物车,第1张

概述packagecom.example.gouwuche;importandroidx.annotation.NonNull;importandroidx.appcompat.app.AppCompatActivity;importandroid.content.ContentValues;importandroid.content.Context;importandroid.content.Intent;importandroid.database.Cursor;impor
package com.example.gouwuche;import androIDx.annotation.NonNull;import androIDx.appcompat.app.AppCompatActivity;import androID.content.ContentValues;import androID.content.Context;import androID.content.Intent;import androID.database.Cursor;import androID.database.sqlite.sqliteDatabase;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ArrayAdapter;import androID.Widget.BaseAdapter;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.ListVIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener {    Intent intent = new Intent();    EditText name,price,number;    button add;    ListVIEw lv;    private MyHelper helper;    List<sqldata> List = new ArrayList<>();    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        name = (EditText)findVIEwByID(R.ID.et_1);        price = (EditText)findVIEwByID(R.ID.et_2);        number = (EditText)findVIEwByID(R.ID.et_3);        add = (button)findVIEwByID(R.ID.add);        lv = (ListVIEw)findVIEwByID(R.ID.List);        helper = new MyHelper(this);    }    public voID add_Click(VIEw vIEw){        sqldata date = new sqldata(name.getText().toString(),price.getText().toString(),number.getText().toString());        insert(date);        Toast.makeText(MainActivity.this,name.getText().toString()+"已添加到购物车",Toast.LENGTH_SHORT).show();    }    public voID delete_Click(VIEw vIEw){        int num = delete(name.getText().toString());        if (num > 0){            Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show();        }else{            Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show();        }    }    public voID update_Click(VIEw vIEw){        update(name.getText().toString(),price.getText().toString(),number.getText().toString());    }    public voID find_Click(VIEw vIEw){        find(name.getText().toString());    }    @OverrIDe    public voID onClick(VIEw v) {    }    //添加到购物车    public long insert(sqldata sqldata){        long ID = 0;        sqliteDatabase db =null;        try {            db = helper.getWritableDatabase();            ContentValues values = new ContentValues();            values.put("name",sqldata.getname());            values.put("price",sqldata.getPrice());            values.put("number",sqldata.getNumber());            db.insert("car",null,values);        } catch (Exception e) {            e.printstacktrace();        } finally {            if (db != null){                db.close();            }        }        return ID;    }    //从购物车中删除    public int delete(String name){        sqliteDatabase db = null;        int num = 0;        try {            db = helper.getWritableDatabase();            num = db.delete("car","name = ?",new String[]{name});        } catch (Exception e) {            e.printstacktrace();        } finally {            if (db!=null){                db.close();            }        }        return num;    }    //查看购物车    public List<sqldata> find(String name){        sqliteDatabase db = null;        db = helper.getReadableDatabase();        MyBaseAdapter myBaseAdapter = new MyBaseAdapter(this,R.layout.item,List);        try {            Cursor cursor = db.query("car",null,null,null,null,null,null);            if (cursor.getCount()!=0){                while (cursor.movetoNext()){                    sqldata find = new sqldata();                    find.setname(cursor.getString(cursor.getColumnIndex("name")));                    find.setPrice(cursor.getString(cursor.getColumnIndex("price")));                    find.setNumber(cursor.getString(cursor.getColumnIndex("number")));                    List.add(find);                    lv.setAdapter(myBaseAdapter);                }            }            cursor.close();        } catch (Exception e) {            e.printstacktrace();        } finally {            if (db != null){                db.close();            }        }        return List;    }    //对购物车进行修改    public int update(String name,String price,String number){        sqliteDatabase db = null;        int num = 0;        try {            db = helper.getWritableDatabase();            ContentValues values = new ContentValues();            values.put("price",price);            values.put("number",number);            num = db.update("car",values,"name = ?",new String[]{name});        } catch (Exception e) {            e.printstacktrace();        } finally {            if (db!=null){                db.close();            }        }        return num;    }    class MyBaseAdapter extends BaseAdapter {        public MyBaseAdapter(MainActivity mainActivity, int List_item, List<sqldata> show) {        }        @OverrIDe        public int getCount() {            return List.size();        }        @OverrIDe        public Object getItem(int position) {            return null;        }        @OverrIDe        public long getItemID(int position) {            return position;        }        @OverrIDe        public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) {            VIEw vIEw = VIEw.inflate(MainActivity.this,R.layout.item,null);            TextVIEw tname = (TextVIEw)vIEw.findVIEwByID(R.ID.t_name);            TextVIEw tprice = (TextVIEw)vIEw.findVIEwByID(R.ID.t_price);            TextVIEw tnumber = (TextVIEw)vIEw.findVIEwByID(R.ID.t_number);            tname.setText(List.get(position).getname());            tprice.setText(List.get(position).getPrice());            tnumber.setText(List.get(position).getNumber());            return vIEw;        }    }}
package com.example.gouwuche;import androID.content.Context;import androID.database.sqlite.sqliteDatabase;import androID.database.sqlite.sqliteOpenHelper;import androIDx.annotation.Nullable;public class MyHelper extends sqliteOpenHelper {    public MyHelper(@Nullable Context context) {        super(context, "car.db", null, 1);    }    @OverrIDe    public voID onCreate(sqliteDatabase db) {        db.execsql("create table car(_ID integer primary key autoincrement,name varchar(50),price varchar(50),number varchar(50))");    }    @OverrIDe    public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {    }}
package com.example.gouwuche;import androIDx.appcompat.app.AppCompatActivity;import androID.content.Intent;import androID.os.Bundle;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class SecondActivity extends AppCompatActivity {    Intent intent = new Intent();    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_second);        intent=getIntent();        String n = intent.getStringExtra("name");        String u = intent.getStringExtra("number");        Toast.makeText(SecondActivity.this,"数据已传送,姓名:"+n+"电话号码:"+u,Toast.LENGTH_SHORT).show();    }}
package com.example.gouwuche;public class sqldata {    private int ID;    private String name,price,number;    public sqldata() {    }    public sqldata(int ID, String name, String price, String number) {        this.ID = ID;        this.name = name;        this.price = price;        this.number = number;    }    public sqldata(String name, String price, String number) {        this.name = name;        this.price = price;        this.number = number;    }    public int getID() {        return ID;    }    public voID setID(int ID) {        this.ID = ID;    }    public String getname() {        return name;    }    public voID setname(String name) {        this.name = name;    }    public String getPrice() {        return price;    }    public voID setPrice(String price) {        this.price = price;    }    public String getNumber() {        return number;    }    public voID setNumber(String number) {        this.number = number;    }}
<?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="vertical"    tools:context=".MainActivity">    <TextVIEw        androID:ID="@+ID/textVIEw"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:background="#FF5722"        androID:gravity="center_horizontal"        androID:text="购物车"        androID:textSize="24sp" />    <tableLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent">        <tableRow            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:gravity="center_horizontal">            <TextVIEw                androID:ID="@+ID/textVIEw2"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:text="物品名:" />            <EditText                androID:ID="@+ID/et_1"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:ems="10"                androID:inputType="textPersonname" />        </tableRow>        <tableRow            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:gravity="center_horizontal">            <TextVIEw                androID:ID="@+ID/textVIEw4"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:text="价格:" />            <EditText                androID:ID="@+ID/et_2"                androID:layout_wIDth="match_parent"                androID:layout_height="wrap_content"                androID:ems="10"                androID:inputType="textPersonname" />        </tableRow>        <tableRow            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:gravity="center_horizontal">            <TextVIEw                androID:ID="@+ID/textVIEw5"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:text="数量:" />            <EditText                androID:ID="@+ID/et_3"                androID:layout_wIDth="wrap_content"                androID:layout_height="wrap_content"                androID:ems="10"                androID:inputType="textPersonname" />        </tableRow>    </tableLayout>    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:orIEntation="horizontal">        <button            androID:ID="@+ID/add"            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:layout_weight="1"            androID:onClick="add_Click"            androID:text="加入购物车" />        <button            androID:ID="@+ID/delete"            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:layout_weight="1"            androID:onClick="delete_Click"            androID:text="从购物车中删除" />        <button            androID:ID="@+ID/query"            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:layout_weight="1"            androID:onClick="find_Click"            androID:text="查看购物车" />        <button            androID:ID="@+ID/update"            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:layout_weight="1"            androID:onClick="update_Click"            androID:text="修改购物车中物品" />    </linearLayout>    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:orIEntation="horizontal">        <TextVIEw            androID:ID="@+ID/textVIEw3"            androID:layout_wIDth="40dp"            androID:layout_height="wrap_content"            androID:layout_weight="1"            androID:gravity="center_horizontal"            androID:text="物品" />        <TextVIEw            androID:ID="@+ID/textVIEw6"            androID:layout_wIDth="40dp"            androID:layout_height="wrap_content"            androID:layout_weight="1"            androID:gravity="center_horizontal"            androID:text="价格" />        <TextVIEw            androID:ID="@+ID/textVIEw7"            androID:layout_wIDth="40dp"            androID:layout_height="wrap_content"            androID:layout_weight="1"            androID:gravity="center_horizontal"            androID:text="数量" />    </linearLayout>    <linearLayout        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent"        androID:orIEntation="vertical">        <ListVIEw            androID:ID="@+ID/List"            androID:layout_wIDth="match_parent"            androID:layout_height="match_parent"            androID:layout_weight="1" />    </linearLayout></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="match_parent"    tools:context=".SecondActivity">    <TextVIEw        androID:ID="@+ID/tn"        androID:layout_wIDth="150dp"        androID:layout_height="wrap_content" />    <TextVIEw        androID:ID="@+ID/tp"        androID:layout_wIDth="100dp"        androID:layout_height="wrap_content" />    <TextVIEw        androID:ID="@+ID/tnu"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content" /></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">    <TextVIEw        androID:ID="@+ID/t_name"        androID:layout_wIDth="40dp"        androID:layout_height="30dp"        androID:layout_weight="1"        androID:background="@drawable/txt_shap"        androID:gravity="@R_502_6823@"        androID:textSize="18sp"        androID:textStyle="bold" />    <TextVIEw        androID:ID="@+ID/t_price"        androID:layout_wIDth="40dp"        androID:layout_height="30dp"        androID:layout_weight="1"        androID:background="@drawable/txt_shap"        androID:gravity="right"        androID:textSize="18sp" />    <TextVIEw        androID:ID="@+ID/t_number"        androID:layout_wIDth="40dp"        androID:layout_height="30dp"        androID:layout_weight="1"        androID:background="@drawable/txt_shap"        androID:gravity="@R_502_6823@"        androID:textSize="18sp" /></linearLayout>

 

总结

以上是内存溢出为你收集整理的购物车全部内容,希望文章能够帮你解决购物车所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1056116.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存