购物车

购物车

package com.example.myapplication;

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.myapplication;

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.myapplication;

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="#8BC34A"
        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"
    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="left"
        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="left"
        android:textSize="18sp" />

</LinearLayout>

 

 

 

posted @ 2020-12-16 10:59  LILY十一啦  阅读(53)  评论(0编辑  收藏  举报