首先呢,要和大家说明一件令我挺尴尬的事情。那就是这篇博文里的代码我写了两遍,至于原因,肯定不是因为我向多加巩固,而是我在量一片电脑上原先敲的代码因为我自己的原因,保存不当导致打不开了....所以现在这篇博文里的代码其实是第二次敲得......
好了,这次我们设计的是一个商品展示案例。要求我们开发一个购物车,需要将购物车中的商品以列表的形式展示,并且还需要对购物车中的商品进行增删改查操作。本次功能的实现需要使用ListView和数据库。
下面给大家提供部分代码:
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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="8dp"
tools:context="cn.edu.bzu.shopshow.MainActivity">
<LinearLayout
android:id="@+id/addLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="商品名称"
android:layout_weight="1"
android:id="@+id/nameET" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="金额"
android:layout_weight="1"
android:id="@+id/balanceET" />
<ImageView
android:onClick="add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_input_add"
android:id="@+id/addIV" />
</LinearLayout>
<ListView
android:id="@+id/accountLV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/addLL">
</ListView>
</LinearLayout>
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">
<TextView
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvId"
android:textSize="20sp"
android:layout_weight="1" />
<TextView
android:text="商品名称"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvName"
android:textSize="20sp"
android:layout_weight="2" />
<TextView
android:text="金额"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvAmount"
android:textSize="20sp"
android:layout_weight="2" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ivUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_up_float"/>
<ImageView
android:id="@+id/ivDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_down_float"/>
</LinearLayout>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@android:drawable/ic_menu_delete"/>
</LinearLayout>
dao//MyHelper.java
package cn.edu.bzu.shopshow.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.Settings;
/**
* Created by Administrator on 2017/4/28.
*/
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context,"itcast.db",null,2);
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("onCreate");
db.execSQL("CREATE TABLE account(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),balance INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("onUpgrade");
}
}
GoodsDao:
package com.example.administrator.shopshowdemo.dao; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.administrator.shopshowdemo.db.DBHelper; import com.example.administrator.shopshowdemo.entity.Goods; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2017/4/27. */ public class GoodsDao { private DBHelper dbHelper; public GoodsDao(Context context){ dbHelper=new DBHelper(context,1); } public void add(Goods goods){ SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase(); ContentValues valuse= new ContentValues(); valuse.put("name",goods.getName()); valuse.put("amount",goods.getAmount()); long id=sqLiteDatabase.insert("gooods",null,valuse); goods.setId(id); sqLiteDatabase.close(); } public int delete(long id){ SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase(); int count=sqLiteDatabase.delete("goods","_id-?",new String[]{id+""}); sqLiteDatabase.close(); return count; } public int update(Goods goods){ SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase(); ContentValues valuse= new ContentValues(); valuse.put("name",goods.getName()); valuse.put("amount",goods.getAmount()); int count=sqLiteDatabase.update("goods",valuse,"_id+?",new String[]{goods.getId()+""}); sqLiteDatabase.close(); return count; } public List<Goods> queryAll(){ List<Goods> goodsList=new ArrayList<>(); SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase(); Cursor cursor=sqLiteDatabase.query("goods",null,null,null,null,null,"amount desc"); while (cursor.moveToNext()){ long id=cursor.getLong(cursor.getColumnIndex("_id")); String name=cursor.getString(cursor.getColumnIndex("name")); int amount=cursor.getInt(cursor.getColumnIndex("amount")); Goods goods=new Goods(id,name,amount); goodsList.add(goods); } cursor.close(); sqLiteDatabase.close(); return goodsList; } }这就是我所编写的代码,有一些乱,大家请忽视我...