一、实验要求
开发一个购物车,需要将购物车中的商品以列表的形式展示,并且还需要对购物车中的商品进行增删改查操作。要实现这些功能就需要使用ListView和SQLite数据库。
二、运行效果图
三、训练目标
(1)、掌握SQLite存储方式
(2)、掌握如何使用SQLite完成CRUD操作
四、用到的class文件和布局文件
五、具体实现代码
(1)“商品展示”程序对应的布局文件(activity_main.xml)代码如下所示:
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="bzu.edu.cn.lab7_4.MainActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="商品名称"
android:id="@+id/EdName"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="金额"
android:id="@+id/EdBalance"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/ic_input_add"
android:id="@+id/IVAdd"
android:onClick="addProduct"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv_product"/>
</LinearLayout>
</LinearLayout>
(2