activity_main.xml

<?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"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/E_group_2"android:orientation="vertical"android:padding="10dp"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="15dp"android:background="@android:color/white"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"style="@style/textViewStyle"android:text="名称:" /><EditTextandroid:id="@+id/et_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@null"android:padding="10dp"android:maxLines="1"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="15dp"android:background="@android:color/white"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"style="@style/textViewStyle"android:text="价格:" /><EditTextandroid:id="@+id/et_price"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@null"android:padding="10dp"android:maxLines="1"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:background="@android:color/white"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"style="@style/textViewStyle"android:text="数量:" /><EditTextandroid:id="@+id/et_number"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:background="@null"android:padding="10dp"android:maxLines="1"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="10dp"><Buttonandroid:id="@+id/add"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"android:text="添加"/><Buttonandroid:id="@+id/query"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"android:text="查询"/><Buttonandroid:id="@+id/update"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"android:text="修改"/><Buttonandroid:id="@+id/delete"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"android:text="删除"/></LinearLayout><LinearLayoutandroid:background="#ffffff"android:layout_marginTop="10dp"android:padding="10dp"android:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:text="名称"style="@style/listViewStyle"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"></TextView><TextViewandroid:text="价格"style="@style/listViewStyle"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"></TextView><TextViewandroid:text="数量"style="@style/listViewStyle"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"></TextView></LinearLayout><Viewandroid:layout_height="1px"android:layout_width="fill_parent"></View><ListViewandroid:id="@+id/listView"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#ffffff"></ListView>
</LinearLayout>

listView_item.xml

<?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="horizontal"android:padding="10dp"><TextViewandroid:id="@+id/tv_name"android:text="测试文本"android:layout_weight="1"style="@style/listViewStyle"android:layout_width="0dp"android:layout_height="wrap_content"
/><TextViewandroid:id="@+id/tv_price"android:text="测试文本"style="@style/listViewStyle"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content" /><TextViewandroid:id="@+id/tv_number"android:text="测试文本"style="@style/listViewStyle"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content" />
</LinearLayout>

Goods.java

public class Goods {private String name;private String price;private String count;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 getCount() {return count;}public void setCount(String count) {this.count = count;}
}

DbHelper

public class DbHelper extends SQLiteOpenHelper {SQLiteDatabase db;ContentValues contentValues=new ContentValues();public DbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {super(context, name, factory, version);db=this.getWritableDatabase();}@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("create table data(id Integer primary key autoincrement,name text,price text,count text )");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}public boolean add(String name, String price, String count) {contentValues.put("name",name);contentValues.put("price",price);contentValues.put("count",count);long flag=db.insert("data",null,contentValues);return flag>0?true:false;}public List<Goods> query() {List<Goods> list=new ArrayList<>();Cursor cursor=db.query("data",null,null,null,null,null,null);if (cursor!=null){while (cursor.moveToNext()){Goods goods=new Goods();goods.setName(cursor.getString(1));goods.setPrice(cursor.getString(2));goods.setCount(cursor.getString(3));list.add(goods);}}return list;}public boolean update(String name, String price, String count) {contentValues.put("name",name);contentValues.put("price",price);contentValues.put("count",count);long flag=db.update("data",contentValues,"name=?",new String[]{name});return flag>0?true:false;}public List<Goods> get(String name) {List<Goods> list=new ArrayList<>();Cursor cursor=db.query("data",null,"name=?",new String[]{name},null,null,null);if (cursor!=null){while (cursor.moveToNext()){Goods goods=new Goods();goods.setName(cursor.getString(1));goods.setPrice(cursor.getString(2));goods.setCount(cursor.getString(3));list.add(goods);}}return list;}public boolean delete(String name) {long flag=db.delete("data","name=?",new String[]{name});return flag>0?true:false;}
}

MyAdapter

public class MyAdapter extends BaseAdapter {private List<Goods> list;private LayoutInflater layoutInflater;public MyAdapter(List<Goods> list, Context context) {this.list=list;this.layoutInflater =LayoutInflater.from(context);}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder=null;if (convertView==null){viewHolder=new ViewHolder();convertView=layoutInflater.inflate(R.layout.listview_item,null,false);viewHolder.tv_showCount=convertView.findViewById(R.id.tv_number);viewHolder.tv_showName=convertView.findViewById(R.id.tv_name);viewHolder.tv_showPrice=convertView.findViewById(R.id.tv_price);convertView.setTag(viewHolder);}else {viewHolder=(ViewHolder) convertView.getTag();}Goods goods=(Goods) getItem(position);viewHolder.tv_showPrice.setText(goods.getPrice());viewHolder.tv_showName.setText(goods.getName());viewHolder.tv_showCount.setText(goods.getCount());return convertView;}class ViewHolder{TextView tv_showName,tv_showPrice,tv_showCount;}
}

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener{private EditText et_name,et_price,et_count;private Button bt_add,bt_update,bt_delete,bt_query;private ListView lv_show;private DbHelper dbHelper;private String name,price,count;private List<Goods> goodsList;private MyAdapter myAdapter;private List<Goods> findList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();dbHelper=new DbHelper(MainActivity.this,"data.db",null,1);if (goodsList!=null){goodsList.clear();}goodsList=dbHelper.query();myAdapter=new MyAdapter(goodsList,MainActivity.this);lv_show.setAdapter(myAdapter);}@Overridepublic void onClick(View v) {switch (v.getId()){case R.id.delete:name=et_name.getText().toString();if (dbHelper.delete(name)){showToast("删除成功");showListView();}else {showToast("删除失败");}break;case R.id.add:name=et_name.getText().toString();price=et_price.getText().toString();count=et_count.getText().toString();if (name==null){showToast("名称不能为空!");}else {if (dbHelper.add(name,price,count)){showToast("添加成功");showListView();}else {showToast("添加失败");}}break;case R.id.query:name=et_name.getText().toString();if (findList!=null){findList.clear();}findList=dbHelper.get(name);myAdapter=new MyAdapter(findList,MainActivity.this);lv_show.setAdapter(myAdapter);break;case R.id.update:name=et_name.getText().toString();price=et_price.getText().toString();count=et_count.getText().toString();if (dbHelper.update(name,price,count)){showToast("修改成功");showListView();}else {showToast("修改失败,名称不能修改");}break;}}private void init() {et_count=findViewById(R.id.et_number);et_name=findViewById(R.id.et_name);et_price=findViewById(R.id.et_price);bt_add=findViewById(R.id.add);bt_delete=findViewById(R.id.delete);bt_query=findViewById(R.id.query);bt_update=findViewById(R.id.update);lv_show=findViewById(R.id.listView);bt_add.setOnClickListener(this);bt_query.setOnClickListener(this);bt_delete.setOnClickListener(this);bt_update.setOnClickListener(this);}private void showToast(String msg){Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();}private void showListView(){goodsList=dbHelper.query();myAdapter=new MyAdapter(goodsList,MainActivity.this);lv_show.setAdapter(myAdapter);}
}

效果展示



编写一个购物车程序,实现在界面中以列表的形式显示购物车的商品信息,商品信息包括商品名称、价格和数量功能,并能够对购物车中的商品信息进行增删改查相关推荐

  1. 实现图书增删改查和分页显示图书信息

    目录 列:实现图书增删改查和分页显示图书信息 效果截图: 添加图书​ 分页显示 上传图片 修改 实现 导包 数据库连接 db.properties UploadServlet图片上传 封装book数据 ...

  2. ArangoDB 学习笔记(一)简介 | ArangoDB 数据模型和概念 | ArangoDB在Windows下的安装与使用 | Web界面的增删改查

    文章目录 一.ArangoDB 简介 二.ArangoDB 数据模型和概念 2.1 ArangoDB的数据库交互 2.2 ArangoDB的数据模型 2.2.1 文档 Documents 2.2.2 ...

  3. .net mvc html访问数据库,ASP.NET中新建MVC项目并连接SqlServer数据库实现增删改查

    场景 ASP.NET中MVC编程模式简介与搭建HelloWorld项目: 在上面使用MVC搭建起来Hello World项目后,怎样连接SqlServer数据库并实现增删改查. 这里使用的是Visua ...

  4. 【java项目学习笔记】Java学生管理系统(纯后端基础--增删改查)

    学生管理系统 在一所学校中,对学生人员流动的管理是很麻烦的,本案例要求编写一个学生管理系统,实现对学生信息的添加.删除.修改和查询功能.每个功能的具体要求如下: 系统的首页 用于显示系统所有的操作,并 ...

  5. servlet增删改查实例_SpringBoot系列(2)整合MongoDB实现增删改查(完整案例)

    自己本科时候一直使用的是Mysql,目前的课题组使用的是MongoDB,因此就花了一部分时间整理了一下,实现springboot与MongoDB的整合,并且实现基本的增删改查操作,从头到尾给出一个完整 ...

  6. mysql增删改查 dao_MYSQL 之 JDBC(七):增删改查(五) DAO设计模式

    Data Access Object,数据访问对象 what:访问数据信息的类.包含了对数据的CRUD(create.read.update.delete,增删改查)操作,而不包含任何业务相关的信息. ...

  7. vue实现对数据的增删改查(CURD)

    vue实现对数据的增删改查(CURD) 导语: 网上看到一个写的比较好的学习文章,转载分享一下 在管理员的一些后台页面里,个人中心里的数据列表里,都会有对这些数据进行增删改查的操作.比如在管理员后台的 ...

  8. HBase--JavaAPI的操作,创建表修改表,增删改查数据

    DDL: public class HbaseClientDemo {Connection conn = null;@Beforepublic void getConn() throws Except ...

  9. 单链表LinkedList的增删改查

    数组作为数据存储结构有一定的缺陷.在无序数组中,搜索性能差,在有序数组中,插入效率又很低(插入位置后面的元素需要集体后移),而且这两种数组的删除效率(集体前移)都很低,并且数组在创建后,其大小是固定了 ...

最新文章

  1. 解决SecureCRT 链接服务器 中文显示出现乱码【有图有真相】
  2. Python 之 Pandas (六)合并
  3. Windows 7 延长支持服务价格曝光:一台电脑最低25美元
  4. html方框中能放置图片么,html中的img标签你不知道的那些细节!
  5. 【蓝桥杯】基础练习 数列排序
  6. Bash 中的 $0 在什么时候不是 argv[0]
  7. 自定义依赖注解无效_最详细的自定义Spring Boot Starter开发教程
  8. h710阵列卡支持最大硬盘_DELL服务器RAID磁盘阵列在线扩容(以H710P阵列卡为例)
  9. .windbg-k*实例分析(查看调用栈分析)
  10. win10系统崩溃怎么修复_系统崩溃怎么重装系统图文教程
  11. Java与C语言混合编程
  12. 535 Login Fail. Please enter your authorization code to login. More information in http://service.ma
  13. 02-07 Python库-pytest
  14. 进程之 回收子进程之避免僵尸进程的产生
  15. 不狂热不忧虑:观看波士顿动力机器人视频的正确姿势
  16. 如何将电脑网页准考证下载地址
  17. PayPal怎么提现结汇,美金一直在PayPal压着?
  18. spark学习之执行计划explain
  19. 离散数学 用c++实现离散数学逻辑推理
  20. java导出excel包含图片

热门文章

  1. 按键控制LED灯开关
  2. 华为系统wifi服务器失败是怎么回事儿,wifi 用云服务器异常
  3. python链接sql报错_python3.7连接sqlserver数据库失败报错20002, DB-Lib error message 20002
  4. php ppt如何转换成pdf,PHP将Word,Wps,Excel,PPT转成PDF
  5. 使用Python进行12306抢票
  6. 《德鲁克管理思想精要》读书笔记8 - 时间
  7. vscode 管理员权限 运行终端
  8. 自动化工具之UIAutomator
  9. Centos7快速搭建服务器加速
  10. 范蠡传(司马迁-史记)