zoukankan      html  css  js  c++  java
  • 第十周作业

    购物车

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

      

  • 相关阅读:
    立方体
    file 图片预览
    [LeetCode][JavaScript]Single Number III
    [LeetCode][JavaScript]Longest Substring Without Repeating Characters
    [LeetCode][JavaScript]Missing Number
    [LeetCode][JavaScript]Course Schedule II
    [LeetCode][JavaScript]Course Schedule
    [LeetCode][JavaScript]Ugly Number II
    [LeetCode][JavaScript]Ugly Number
    [LeetCode][JavaScript]Single Number II
  • 原文地址:https://www.cnblogs.com/1774ax/p/14170211.html
Copyright © 2011-2022 走看看