1.创建程序
首先创建一个名为”商品展示“的应用程序,将包名修改为cn.edu.bzu.shopshowdemo。设计用户交互页面。
"商品展示"程序对应的布局文件(activity_main.xml)如下所示:
2.创建ListView Item布局
由于本案例用到ListView布局,因此需要编写一个ListView Item的布局。在res/layout目录下创建一个item.xml文件:
3.创建数据库
创建数据库属于数据操作,因此需要在cn.edu.bzu.shopshowdemo的包下创建一个名为db的包,并在该包下定义一个DBhelper类继承自SQLOpenHelper。创建的数据库如下:
4.创建数据操作逻辑类
在cn.edu.bzu.shopshowdemo包下创建一个GoodsDao类用于操作数据,如下所示:
5.创建GoodsAdapter.java,如下图:
6.编写交互代码(MainActivity)
数据库的操作完成之后需要界面与数据库进行交互,用于实现数据库中的数据以ListView的形式展示在页面上,如下所示:
7.运行程序展示商品
添加:
删除:
增加金额:
减少金额:
代码如下:
1.activity_main.xml布局文件中的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="cn.edu.bzu.shopshowdemo.MainActivity">
<LinearLayout
android:id="@+id/addLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="商品名称"
android:inputType="text" />
<EditText
android:id="@+id/etAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="商品金额"
android:inputType="number" />
<ImageView
android:id="@+id/ivAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addGoods"
android:src="@android:drawable/ic_input_add" />
</LinearLayout>
<ListView
android:id="@+id/lvGoods"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/addLL"></ListView>
</RelativeLayout>
2.items.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:id="@+id/tvId" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:gravity="left" android:text="1" android:textSize="20sp" /> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left" android:maxLines="1" android:text="商品名称" android:textSize="20sp" /> <TextView android:id="@+id/tvAmount" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:gravity="left" android:maxLines="1" android:text="金额" android:textSize="20sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <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:id="@+id/ivDelete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/ic_menu_delete" /> </LinearLayout>
3.GoodsDao类中的文件代码:
package cn.edu.bzu.shopshowdemo.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import cn.edu.bzu.shopshowdemo.db.DBhelper;
import cn.edu.bzu.shopshowdemo.entity.Goods;
/**
* 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 values = new ContentValues();
values.put("name", goods.getName());
values.put("amount", goods.getAmount());
long id= sqLiteDatabase.insert("goods", null, values);
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 values = new ContentValues();
values.put("name", goods.getName());
values.put("amount", goods.getAmount());
int count = sqLiteDatabase.update("goods", values, "_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;
}
}
4.DBhelper.java文件中的程序代码:
package cn.edu.bzu.shopshowdemo.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Administrator on 2017/4/27.
*/
public class DBhelper extends SQLiteOpenHelper{
public static final String CREATE_GOODS="create table goods(_id integer primary key autoincrement,name varchar(20),amount integer)";
public DBhelper(Context context, int version) {
super(context, "goods.db", null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_GOODS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
}
5.GoodsAdapter.java文件中的程序代码:
package cn.edu.bzu.shopshowdemo;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.support.annotation.NonNull;
import android.support.annotation.PluralsRes;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import cn.edu.bzu.shopshowdemo.dao.GoodsDao;
import cn.edu.bzu.shopshowdemo.entity.Goods;
/**
* Created by Administrator on 2017/4/27.
*/
public class GoodsAdapter extends ArrayAdapter<Goods>{
private int resounceId;
private GoodsDao goodsDao;
private List<Goods> goodsList;
public GoodsAdapter(Context context, int resource, List<Goods> objects, GoodsDao goodsDao) {
super(context, resource, objects);
resounceId=resource;
goodsList =objects;
this.goodsDao=goodsDao;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Goods goods=getItem(position);
View view =null;
ViewHolder viewHolder;
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resounceId,null);
viewHolder=new ViewHolder();
viewHolder.tvId= (TextView) view.findViewById(R.id.tvId);
viewHolder.tvName= (TextView) view.findViewById(R.id.tvName);
viewHolder.tvAmount= (TextView) view.findViewById(R.id.tvAmount);
viewHolder.ivUp= (ImageView) view.findViewById(R.id.ivUp);
viewHolder.ivDown= (ImageView) view.findViewById(R.id.ivDown);
viewHolder.ivDelete= (ImageView) view.findViewById(R.id.ivDelete);
view.setTag(viewHolder);
}else{
view =convertView;
viewHolder= (ViewHolder) view.getTag();
}
viewHolder.tvId.setText(goods.getId()+"");
viewHolder.tvName.setText(goods.getName());
viewHolder.tvAmount.setText(goods.getAmount()+"");
viewHolder.ivDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
builder.setTitle("你确定要删除吗?");
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
goodsList.remove(goods);
goodsDao.delete(goods.getId());
notifyDataSetChanged();
}
});
builder.setNegativeButton("cancel",null);
builder.show();
}
});
viewHolder.ivUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
goods.setAmount(goods.getAmount()-1);
goodsDao.update(goods);
notifyDataSetChanged();
}
});
viewHolder.ivDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
goods.setAmount(goods.getAmount()+1);
goodsDao.update(goods);
notifyDataSetChanged();
}
});
return view;
}
class ViewHolder{
TextView tvId;
TextView tvName;
TextView tvAmount;
ImageView ivUp;
ImageView ivDown;
ImageView ivDelete;
}
}
6.MainActivity.java中的文件代码:
package cn.edu.bzu.shopshowdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
import cn.edu.bzu.shopshowdemo.dao.GoodsDao;
import cn.edu.bzu.shopshowdemo.entity.Goods;
public class MainActivity extends AppCompatActivity {
private EditText etName;
private EditText etAmount;
private ListView lvGoods ;
private GoodsAdapter goodsAdapter;
private GoodsDao goodsDao;
private List<Goods> goodsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName= (EditText) findViewById(R.id.etName);
etAmount= (EditText) findViewById(R.id.etAmount);
lvGoods = (ListView) findViewById(R.id.lvGoods);
goodsDao = new GoodsDao(this);
goodsList= goodsDao.queryALL();
goodsAdapter =new GoodsAdapter(this,R.layout.item,goodsList,goodsDao);
lvGoods.setAdapter(goodsAdapter);
}
public void addGoods(View view){
Toast.makeText(this,"添加成功",Toast.LENGTH_LONG).show();
String name =etName.getText().toString();
String amount = etAmount.getText().toString();
if(TextUtils.isEmpty(name)||TextUtils.isEmpty(amount))
return;
Goods goods = new Goods(name,Integer.parseInt(amount));
goodsDao.add(goods);
goodsList.add(goods);
goodsAdapter.notifyDataSetChanged();
etName.setText("");
etAmount.setText("");
Toast.makeText(this,"添加成功",Toast.LENGTH_LONG).show();
}
}
出现的问题:
delete按钮与up、down按钮不处于同一水平位置?
解决方案:
将delete按钮放在up、down按钮线性布局的外面