• 第十次作业—listview+sqlite 购物车


    数据库继承类mydbhelpe

    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import androidx.annotation.Nullable;
    public class mydbhelp extends SQLiteOpenHelper {
        public mydbhelp(@Nullable Context context) {
            super(context, "jwc.db", null, 1);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
            String sql = "CREATE TABLE cart(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20),  price VARCHAR(20), number VARCHAR(20))";
            db.execSQL(sql);
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    
    }
    

      list view适配器

    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;
    
    import java.util.List;
    public class myadapter extends BaseAdapter {
        private List<dxl> list;
        private LayoutInflater layoutInflater;
        public myadapter(Context context, List<dxl> list){
            this.layoutInflater = LayoutInflater.from(context);
            this.list = list;
        }
        @Override
        public int getCount() {
            Log.e("yanwenhua","list.size()--"+list.size());
            return list.size();
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder;
            if (convertView==null){
                convertView=layoutInflater.inflate(R.layout.list,null);
                viewHolder=new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else {
                viewHolder=(ViewHolder) convertView.getTag();
            }
            dxl dxl = list.get(position);
            viewHolder.tv_name.setText("商品名称:"+dxl.getName());
            viewHolder.tv_price.setText("商品价格:"+dxl.getPrice());
            viewHolder.tv_number.setText("商品数量:"+dxl.getNumber());
            Log.e("yanwenhua","cartBean.getName()-"+dxl.getName()+"  "+dxl.getPrice()+"  "+dxl.getNumber());
            return convertView;
        }
        class ViewHolder{
            TextView tv_name;
            TextView tv_price;
            TextView tv_number;
            public ViewHolder(View view){
                tv_name = (TextView) view.findViewById(R.id.tv_name);
                tv_price = (TextView) view.findViewById(R.id.tv_price);
                tv_number = (TextView) view.findViewById(R.id.tv_number);
            }
        }
    
    }
    

      自定义面向对象封装类

    package com.tianwengeek.zy1107;
    public class dxl {
        private String name;
        private String price;
        private String number;
        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;
        }
    }
    

      mainactivity。java

    package com.tianwengeek.zy1107;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.Toast;
    
    import java.util.ArrayList;
    import java.util.List;
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
        private EditText et_n,et_p,et_nu;
        private ListView listView;
        private String name,price,number;
        private  mydbhelp mydb;
        private SQLiteDatabase db;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            def();
    
        }
        public  void def(){
            et_n = (EditText)findViewById(R.id.et_name);
            et_p =  (EditText)findViewById(R.id.et_price);
            et_nu =  (EditText) findViewById(R.id.et_number);
            listView = (ListView)findViewById(R.id.listView);
            Button add = (Button)findViewById(R.id.add);
            Button query =(Button) findViewById(R.id.query);
            Button update =(Button) findViewById(R.id.update);
            Button delete = (Button)findViewById(R.id.delete);
            add.setOnClickListener(this);
            query.setOnClickListener(this);
            update.setOnClickListener(this);
            delete.setOnClickListener(this);
            mydb = new mydbhelp(this);
    
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.add:
                    db = mydb.getWritableDatabase();
                    name = et_n.getText().toString();
                    price = et_p.getText().toString();
                    number = et_n.getText().toString();
                    ContentValues values = new ContentValues();
                    values.put("name", name);
                    values.put("price", price);
                    values.put("number", number);
                    db.insert("cart", null, values);
                    db.close();
                    Toast.makeText(this, "已添加购物车", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.query:
                    List<dxl> list = new ArrayList();
                    db = mydb.getWritableDatabase();
                    Cursor cursor = db.query("cart", null, null, null, null,
                            null, null);
                    if (cursor.getCount() == 0) {
                        Toast.makeText(this, "商品不存在", Toast.LENGTH_SHORT).show();
                    } else {
                        while (cursor.moveToNext()) {
                            dxl dy = new dxl();
                            int nameIndex = cursor.getColumnIndex("name");
                            int priceIndex = cursor.getColumnIndex("price");
                            int numberIndex = cursor.getColumnIndex("number");
                            String name = cursor.getString(nameIndex);
                            String price = cursor.getString(priceIndex);
                            String number = cursor.getString(numberIndex);
                            dy.setName(name);
                            dy.setPrice(price);
                            dy.setNumber(number);
                            list.add(dy);
                        }
                        myadapter adapter = new myadapter(MainActivity.this,list);
                        listView.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                    cursor.close();
                    db.close();
                    break;
                case  R.id.update:
                    name = et_n.getText().toString();
                    price = et_p.getText().toString();
                    number = et_n.getText().toString();
                    db = mydb.getWritableDatabase();
                    values = new ContentValues();
                    values.put("number",number);
                    values.put("price",price);
                    db.update("cart", values, "name=?",
                            new String[]{name});
                    db.close();
                    Toast.makeText(this, "商品信息已修改", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.delete:
                    db = mydb.getWritableDatabase();
                    db.delete("cart", null, null);
                    List<dxl> list2 = new ArrayList();
                    myadapter adapter = new myadapter(MainActivity.this,list2);
                    listView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    db.close();
                    Toast.makeText(this, "已清空购物车", Toast.LENGTH_SHORT).show();
                    break;
            }
    
        }
    
    }
    

      UI布局

    <?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="#E6E6E6"
        android:orientation="vertical"
        android:padding="10dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:background="@android:color/white"
            android:orientation="horizontal">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="名称:"
                android:textColor="#000"
                android:textSize="20sp" />
            <EditText
                android:id="@+id/et_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/es"
                android:padding="10dp"
                android:maxLines="1"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="价格:"
                android:textColor="#000"
                android:textSize="20sp" />
            <EditText
                android:id="@+id/et_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/es"
                android:padding="10dp"
                android:maxLines="1"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="数量:"
                android:textColor="#000"
                android:textSize="20sp" />
            <EditText
                android:id="@+id/et_number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/es"
                android:padding="10dp"
                android:maxLines="1"/>
        </LinearLayout>
    </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="40dp">
            <Button
                android:id="@+id/add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bs"
                android:layout_weight="1"
                android:text="添加"/>
            <Button
                android:id="@+id/query"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bs"
                android:text="查询"/>
            <Button
                android:id="@+id/update"
                android:layout_weight="1"
                android:background="@drawable/bs"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="修改"/>
            <Button
                android:id="@+id/delete"
                android:layout_weight="1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/bs"
                android:text="删除"/>
        </LinearLayout>
        <ListView
            android:id="@+id/listView"
            android:layout_marginTop="20sp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="#ffffff">
        </ListView>
    </LinearLayout>
    

      list-menu

    <?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:orientation="vertical"
        android:padding="10dp">
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"/>
        <TextView
            android:id="@+id/tv_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:textSize="15sp"/>
        <TextView
            android:id="@+id/tv_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:textSize="15sp"/>
    
    </LinearLayout>
    

      

  • 相关阅读:
    Ad hoc access to OLE DB provider 'Microsoft.ACE.OLEDB.12.0' has been denied. You must access this provider through a linked server.
    阻塞问题:会话是sleeping的,但是open_tran 不是0
    windows Server DNS服务器配置
    内存缺页
    "ros::NodeHandle"的用法:全局vs.私有
    python 判断当前执行用户是否是 root 用户
    docker 安装及启动 postgresql 及navicat 连接
    Mac 在环境变量中隐藏密码或者密钥等信息
    磁盘空间不足导致虚拟机无法启动
    VirtuaBox 下安装 Centos8 无法上网
  • 原文地址:https://www.cnblogs.com/TSHEN/p/11823871.html
Copyright © 2020-2023  润新知