题 目 商城管理系统设计与实现
系 别 信息与控制工程系
专 业 计算机科学与技术
班 级 1701
学 号 173030112
姓 名 李亚楠
1. 基于Android的商城管理系统设计与实现
1.1 系统需求分析
1.1.1 用例分析
用例图如1.1所示
图1.1 商城用例图
1.1.2 数据逻辑模型
商城管理系统ER图如图1.2所示
n
n n
1 1
n
图1.2 商城管理系统E-R图
1.2 系统总体设计
图1.3 商城管理功能图
在互联网迅猛发展的时代,网上购物已经成为许多人日常生活的一部分,足不出户,便可得天下万物。而在移动平台技术日益成熟的如今,在移动平台上实现网上购物功能更是未来的趋势,时代的潮流。
为了在Android平台上实现移动商城系统,本文以基于Linux的开放源代码操作系统Android为研究对象,进行实际的开发工作。主要任务包括三个方面:第一,在Android系统上开发一款功能强大,可维护性好,用户友好的手机客户端软件;第二,开发一款能有效支持移动平台应用的,稳定,高效的服务端后台系统;第三,寻求一种能够有效的解决移动平台与服务器之间数据交互,同时降低系统消耗的数据传输途径。
论文从项目的背景出发,介绍了基于Android的移动商城系统的开发过程和研究价值,探讨了如何将各项先进技术融入项目的开发过程中。本文以本系统的部分后台开发,前台客户端相关功能的实现,以及前后台之间数据传输的开发过程为例,详细的描述了移动商城系统的设计和实现的过程,同时从系统开发的需求分析,详细设计到最终的测试等多个方面对系统进行了全方位的描述,并探讨了下一步的改进计划。
1.3 系统数据库设计
根据前面E-R图设计出实现系统功能所需要的数据库表的结构,并以表格的形式列出每一个数据库表的表结构,即字段名、类型、长度、小数位数、是否主键、索引、是否允许为空、说明等。并介绍表与表之间的逻辑关系。如表3.1所示:
表3.1 user
字段名 |
数据类型 |
长度 |
主键 |
索引 |
外键 |
可空 |
rowid |
text |
11 |
是 |
|
否 |
否 |
userid |
int |
50 |
否 |
|
否 |
否 |
password |
text |
50 |
否 |
|
否 |
否 |
表3.2 goods
字段名 |
数据类型 |
长度 |
主键 |
索引 |
外键 |
可空 |
rowid |
text |
11 |
是 |
|
否 |
否 |
shopname |
text |
20 |
否 |
|
否 |
否 |
shopintro |
text |
50 |
否 |
|
否 |
否 |
shopprice |
int |
10 |
否 |
|
否 |
否 |
shopnum |
int |
Unknow |
否 |
|
否 |
否 |
shopdeller |
text |
30 |
否 |
|
否 |
否 |
表3.3 buyer_goods(部门表)
字段名 |
数据类型 |
长度 |
主键 |
索引 |
外键 |
可空 |
rowid |
int |
11 |
是 |
|
否 |
否 |
id |
int |
20 |
否 |
|
否 |
否 |
buyerid |
int |
50 |
否 |
|
否 |
否 |
buyershopnum |
text |
50 |
否 |
|
否 |
否 |
shopname |
text |
50 |
否 |
|
否 |
否 |
1.4 系统详细设计
1.4.1 商家模块
表3.4 商家模块
编号 |
输入 |
处理 |
输出 |
1 |
用户名和密码 |
根据输入用户名从数据库中找一个同名的用户并且进行密码比对 |
成功则跳转到添加商品,失败就转到登录页面 |
2 |
用户名和密码 |
向数据库中插入一个新的商家用户 |
成功则提示失败则转到添加页面 |
1.4.2 顾客模块
表3.10 部门管理模块
编号 |
输入 |
处理 |
输出 |
1 |
用户名和密码 |
向数据库中插入一个新的顾客用户 |
成功则提示失败则转到添加页面 |
2 |
充值 |
输入充值金额 |
成功则提示充值成功 |
3 |
购买 |
点击要购买商品 |
成功点击已购商品显示购买的商品则购买成功 |
1.4.3 登录设计
界面设计的时候用了toast提示,然后用intent切换页面。登录注册页面如下图3.4 所示
图3.4 登录注册页面图
1.5 系统实现
1.5.1 注册模块
注册包括商家注册和顾客注册。
1.5.1.1 顾客注册
顾客注册包括输入账号,输入密码,输入充值金额之后点顾客,最后点击注册。
1.5.1.2 商家注册
商家注册包括输入账号,输入密码,输入充值金额之后点商家,最后点击注册。
顾客和商家注册代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
try {
// 打开数据库,如果数据库不存在则创建数据库
mSQLiteDatabase = this.openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
} catch (Exception ex) {
Toast.makeText(this, “打开或创建数据库异常:” + ex.getMessage(), 2000).show();
}
edt_userid = (EditText)this.findViewById(R.id.edt_userid);
edt_userpwd = (EditText)this.findViewById(R.id.edt_userpwd);
edt_usermoney= (EditText)this.findViewById(R.id.edt_usermoney);
Button bt3 = (Button) findViewById(R.id.button3);
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ShopregActivity.this.finish();
}
});
Button button_register = (Button) this.findViewById(R.id.button_register);
button_register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String userid = edt_userid.getText().toString();
String userpwd = edt_userpwd.getText().toString();
int usermoney=Integer.parseInt(edt_usermoney.getText().toString());
RadioButton radio_buyer =(RadioButton) findViewById(R.id.radio_buyer);
String type = “seller”;
if(radio_buyer.isChecked()){
type = “buyer”;
}
try{
mSQLiteDatabase.execSQL(“insert into user values(‘”+userid+”‘,'”+userpwd+”‘,'”+usermoney+”‘,'”+type+”‘)”);
Toast.makeText(ShopregActivity.this, “注册成功!”, 2000).show();
}catch(Exception e){
Toast.makeText(ShopregActivity.this, “新增用户失败:” + e.getMessage(), 2000).show();
}
}
});
}
1.5.2 商家登录模块
添加商品的名称价格数量,之后点击发布商品。
商家发布商品代码: @Override
public void onClick(View v) {
// TODO Auto-generated method stub
String shopname = edit_shopname.getText().toString();
String shopintro = edit_shopintro.getText().toString();
int shopprice = Integer.parseInt(edit_shopprice.getText()
.toString());
int shopnum = Integer.parseInt(edit_shopnum.getText()
.toString());
String sql = “insert into goods values(‘” + shopname + “‘,'”
+ shopintro + “‘,”+shopprice + “,” + shopnum + “,'” + userId + “‘)”;
try {
// 打开数据库,如果数据库不存在则创建数据库
mSQLiteDatabase = openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopAddsActivity.this, “添加商品成功!”,
1000).show();
Intent i = new Intent(ShopAddsActivity.this,
null);//SellerCheckShopActivity.class
Bundle b = new Bundle();
b.putString(“user”, userId);
i.putExtra(“data”, b);
startActivity(i);
} catch (Exception e) {
Toast.makeText(ShopAddsActivity.this,
“添加商品异常!” + e.getMessage(), 1000).show();
}
}
});
}
1.5.3 用户登录模块
主要可以查看商家发布的商品名称,自己的余额,如果余额不足可以输入充值金额然后点击充值按钮进行充值。
购买商品代码实现:public void onClick(View v) {
// TODO Auto-generated method stub
String shopname = tv.getText().toString();
buy_shopnum=Integer.parseInt(edt_buy_shopnum.getText().toString());
int price=Integer.parseInt(shopprice);
int total_price=buy_shopnum*price;
try {
String a=”select usermoney from user where userid='”+userId+”‘ ” ;
Cursor cursor = mSQLiteDatabase.rawQuery(a, null);
while (cursor.moveToNext()) {
buyermoney= cursor.getInt(cursor.getColumnIndex(“usermoney”));
}
if(buyermoney>=total_price){
String sql = “update goods set shopnum=shopnum-“+buy_shopnum+” where shopname='”+shopname+”‘ ” ;
mSQLiteDatabase.execSQL(sql);
sql=”update user set usermoney=usermoney-“+total_price+” where userid='”+userId+”‘”;
mSQLiteDatabase.execSQL(sql);
sql = “insert into buyer_goods(buyerid,shopname,buyshopnum) values(‘” + userId + “‘,'”
+ shopname + “‘,'”+buy_shopnum+”‘)”;
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopbSelectActivity.this, “购买成功!”,
1000).show();
Intent i = new Intent(ShopbSelectActivity.this,
ShopbSelectActivity.class);
Bundle b = new Bundle();
b.putString(“user”, userId);
i.putExtra(“data”, b);
startActivity(i);
}
else {Toast.makeText(ShopbSelectActivity.this, “余额不足,请充值!”,
3000).show();}
} catch (Exception e) {
Toast.makeText(ShopbSelectActivity.this,
“添加商品异常!” + e.getMessage(), 1000).show();
}
}
});
充值代码实现:
btn_myshop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(ShopbSelectActivity.this,
ShopbuyerActivity.class);
Bundle b = new Bundle(); //发送数据
b.putString(“user”, userId);
i.putExtra(“data”, b);
startActivity(i);
}
});
edt_addmoney=(EditText)this.findViewById(R.id.edittext_addmoney);
btn_addmoney= (Button)this.findViewById(R.id.button_addmoney);
btn_addmoney.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String addmoney=edt_addmoney.getText().toString();
String sql = “update user set usermoney=usermoney+”+addmoney+” where userid='”+userId+”‘ ” ;
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopbSelectActivity.this, “成功充值”+addmoney+”元!”,
1000).show();
Intent i = new Intent(ShopbSelectActivity.this,
ShopbSelectActivity.class);
Bundle b = new Bundle();
b.putString(“user”, userId);
i.putExtra(“data”, b);
startActivity(i);
}
});
1.5.3.1 查看已购商品
点击查看已购商品,页面跳转到已购商品的页面,页面显示你当前的余额和你所购买的商品名称,商品单价,购买商品数量和你购买时商家的名称。点击返回按钮可以返回到前一个页面。
查看已购商品代码实现:
private void selectData() {
String sql = “select g.shopname,g.shopprice,bd.buyshopnum,g.shopseller from buyer_goods as bd,goods as g where bd.buyerid='”
+ userId + “‘ and bd.shopname=g.shopname”;
Cursor cursor = mSQLiteDatabase.rawQuery(sql, null);
if (cursor != null) {
// 定义存储查询结果的List,每一行数据类型为Map<String, Object>,其中String代表列名,Object为值
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
// 遍历游标对象,每一行获取的数据存放到Map<String, Object>类型的变量map中
while (cursor.moveToNext()) {
map = new HashMap<String, Object>();
map.put(“shopname”, cursor.getString(cursor.getColumnIndex(“shopname”)));
map.put(“shopprice”, cursor.getInt(cursor.getColumnIndex(“shopprice”)));
map.put(“buyshopnum”, cursor.getInt(cursor.getColumnIndex(“buyshopnum”)));
map.put(“shopseller”, cursor.getString(cursor.getColumnIndex(“shopseller”)));
list.add(map);
}
// 创建Adapter,并于ListView绑定
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.buyer_myshop,
new String[] { “shopname”, “shopprice”, “buyshopnum”, “shopseller” },
new int[] { R.id.tv_buyer_myshopname, R.id.tv_buyer_myshopprice, R.id.tv_buyer_myshopnum,
R.id.tv_buyer_mysellername });
lv1.setAdapter(adapter);
}
}
private void selectBuyermoney() {
String sql = “select usermoney from user where userid='” + userId + “‘”;
// 获得查询结果对应的游标对象
Cursor cursor = mSQLiteDatabase.rawQuery(sql, null);
if (cursor != null) {
// 定义存储查询结果的List,每一行数据类型为Map<String, Object>,其中String代表列名,Object为值
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
while (cursor.moveToNext()) {
map = new HashMap<String, Object>();
map.put(“usermoney”, cursor.getInt(cursor.getColumnIndex(“usermoney”)));
list.add(map);
}
// 创建Adapter,并于ListView绑定
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.buyer_money2, new String[] { “usermoney” },
new int[] { R.id.tv_buyer_money2 });
lv2.setAdapter(adapter);
}
}
1.6 系统测试
本次实习所用的开发环境为
开发技术:android
数据库:sqlLite
开发工具:eclipse
1.6.1 项目测试
1、测试登录功能
2、测试注册功能
3、测试添加商品功能
4、测试用户购买充值功能
5、测试查已购商品功能
1.6.2 测试结果
以上截图测试了本系统的大部分功能,并且成功实现了每个功能。
2. 课程设计总结
本次课程设计,让我受益匪浅,这次的课程设计把自己学的java集合利用上去了,非常开心。还有就是虽然上课听老师讲课,但是自己学完做做作业之后就没再看过,现在很多知识都忘记了,通过这次的课程设计我回忆了一遍android的基础东西,而且把java知识复习了,通过这次的android课程设计让我更加了解了这门学科,让自己更有兴趣的去学习这门学科,让我感觉非常开心。
然后就是通过这次的课程设计我感觉到了写代码需要非常严谨,在这次的课程设计中我深有体会,尤其是调用layout中的 控件,在我感觉如果写的不严谨在你调用这个控件时就非常的困难,所以我们在课程设计需要把每个控件的ID设置严谨一点,这样我们才能轻松地调用这个控件,还有就是不能有一点错误,不然整个程序就会不能顺利地运行,还有一点就是需要做到不懂就问的习惯,让老师同学帮助一下这样的话,让你在写代码将会更加的流畅,还有一点就是不论遇到什么困难和难题都需要我们的坚持,在坚持之后我们一定能够成功完成。总之,在这次的课程设计当中我学习到了很多知识,让我也巩固了一下书本上的知识,此次的课程设计在我学习生涯上起到了至关重要的一步。
附录:
商城购物系统的实现
使用技术:
UI设计:建立了5个界面
数据库使用:运用sqlLite数据库建立了3个表分别是用户信息表、商品表和用户购买商品表。
Activity组件的应用:运用了Activity组件实现了登录界面的跳转功能和
查看已购商品的跳转界面。
ListView的使用:使用了2个listview实现了商家添加商品的显示和查看用户已购商品的显示。
UML类图:
代码:
清单文件:
/ShopCenter/src/com/example/shopcenter/MainActivity.java
/ShopCenter/src/com/example/shopcenter/ShopAddsActivity.java
/ShopCenter/src/com/example/shopcenter/ShopbSelectActivity.java
/ShopCenter/src/com/example/shopcenter/ShopbuyerActivity.java
/ShopCenter/src/com/example/shopcenter/ShopregActivity.java
/ShopCenter/res/layout/activity_buyer_check_shop.xml
/ShopCenter/res/layout/activity_buyer_select_shop.xml
/ShopCenter/res/layout/activity_main.xml
/ShopCenter/res/layout/activity_register.xml
/ShopCenter/res/layout/activity_seller_add_shop.xml
/ShopCenter/res/layout/buyer_money.xml
/ShopCenter/res/layout/buyer_money2.xml
/ShopCenter/res/layout/buyer_myshop.xml
/ShopCenter/res/layout/buyer_shop.xml
/ShopCenter/res/layout/shop_buyer.xml
布局文件:
截图:
/ShopCenter/res/layout/activity_buyer_check_shop.xml
/ShopCenter/res/layout/activity_buyer_select_shop.xml
/ShopCenter/res/layout/activity_main.xml
/ShopCenter/res/layout/activity_register.xml
/ShopCenter/res/layout/activity_seller_add_shop.xml
/ShopCenter/res/layout/buyer_money.xml
/ShopCenter/res/layout/buyer_money2.xml
/ShopCenter/res/layout/buyer_myshop.xml
/ShopCenter/res/layout/buyer_shop.xml
/ShopCenter/res/layout/shop_buyer.xml
Java原代码:
主界面
package com.example.shopcenter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
//定义用于访问SQLite的对象
private SQLiteDatabase mSQLiteDatabase = null;
//数据库名称
private static final String DATABASE_NAME = “test.db”;
private static final String CREATE_TABLE_USER=”create table if not exists user(userid text primary key,userpwd text,usermoney integer ,usertype text)”;
private static final String CREATE_TABLE_GOODS=”create table if not exists goods(shopname text primary key,shopintro text,shopprice integer,shopnum integer,shopseller text)”;
private static final String CREATE_TABLE_BUYER_GOODS=”create table if not exists buyer_goods(id integer primary key autoincrement,buyerid text,shopname text,buyshopnum integer)”;
private EditText edt_login_userid,edt_login_userpwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// 打开数据库,如果数据库不存在则创建数据库
mSQLiteDatabase = this.openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
} catch (Exception ex) {
Toast.makeText(this, “打开或创建数据库异常:” + ex.getMessage(), 2000).show();
}
try {
mSQLiteDatabase.execSQL(CREATE_TABLE_USER);
mSQLiteDatabase.execSQL(CREATE_TABLE_GOODS);
mSQLiteDatabase.execSQL(CREATE_TABLE_BUYER_GOODS);
} catch (Exception ex) {
Toast.makeText(this, “创建用户表异常:” + ex.getMessage(), 2000).show();
}
Button goto_register = (Button) this.findViewById(R.id.button_goto_register);
goto_register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,ShopregActivity.class);
startActivity(i);
}
});
edt_login_userid = (EditText)this.findViewById(R.id.edt_login_userid);
edt_login_userpwd = (EditText)this.findViewById(R.id.edt_login_userpwd);
Button user_login = (Button) this.findViewById(R.id.button_login);
user_login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String login_userid = edt_login_userid.getText().toString();
String login_userpwd = edt_login_userpwd.getText().toString();
try{
String sql = “select * from user where userid='”+login_userid+”‘ and userpwd='”+login_userpwd+”‘”;
Cursor cursor = mSQLiteDatabase.rawQuery(sql,null);
if(cursor.moveToNext()){
String type = cursor.getString(cursor.getColumnIndex(“usertype”));
Bundle b = new Bundle();
b.putString(“user”, login_userid);
if(type.equals(“seller”)){
Intent i = new Intent(MainActivity.this,ShopAddsActivity.class);
i.putExtra(“data”, b);
startActivity(i);
}else{
Intent i = new Intent(MainActivity.this,ShopbSelectActivity.class);
i.putExtra(“data”, b);
startActivity(i);
}
}else{
Toast.makeText(MainActivity.this, “账号或密码错误!”, 2000).show();
}
}catch(Exception e){
Toast.makeText(MainActivity.this, e.getMessage(), 2000).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
用户注册类:
package com.example.shopcenter;
import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
public class ShopregActivity extends Activity {
//定义用于访问SQLite的对象
private SQLiteDatabase mSQLiteDatabase = null;
//数据库名称
private static final String DATABASE_NAME = “test.db”;
private EditText edt_userid,edt_userpwd,edt_usermoney;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
try {
// 打开数据库,如果数据库不存在则创建数据库
mSQLiteDatabase = this.openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
} catch (Exception ex) {
Toast.makeText(this, “打开或创建数据库异常:” + ex.getMessage(), 2000).show();
}
edt_userid = (EditText)this.findViewById(R.id.edt_userid);
edt_userpwd = (EditText)this.findViewById(R.id.edt_userpwd);
edt_usermoney= (EditText)this.findViewById(R.id.edt_usermoney);
Button bt3 = (Button) findViewById(R.id.button3);
bt3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ShopregActivity.this.finish();
}
});
Button button_register = (Button) this.findViewById(R.id.button_register);
button_register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String userid = edt_userid.getText().toString();
String userpwd = edt_userpwd.getText().toString();
int usermoney=Integer.parseInt(edt_usermoney.getText().toString());
RadioButton radio_buyer =(RadioButton) findViewById(R.id.radio_buyer);
String type = “seller”;
if(radio_buyer.isChecked()){
type = “buyer”;
}
try{
mSQLiteDatabase.execSQL(“insert into user values(‘”+userid+”‘,'”+userpwd+”‘,'”+usermoney+”‘,'”+type+”‘)”);
Toast.makeText(ShopregActivity.this, “注册成功!”, 2000).show();
}catch(Exception e){
Toast.makeText(ShopregActivity.this, “新增用户失败:” + e.getMessage(), 2000).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.register, menu);
return true;
}
}
商品发布类:
package com.example.shopcenter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ShopAddsActivity extends Activity {
private SQLiteDatabase mSQLiteDatabase = null;
private static final String DATABASE_NAME = “test.db”;
private EditText edit_shopnum, edit_shopname, edit_shopprice,edit_shopintro;
private String userId;
Button button_addshop, button_reset,button_seller_myshop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seller_add_shop);
Intent i = this.getIntent();
Bundle b = i.getBundleExtra(“data”);
userId = b.getString(“user”);
edit_shopname = (EditText) findViewById(R.id.edt_shopname);
edit_shopintro = (EditText) findViewById(R.id.edt_shopintro);
edit_shopprice = (EditText) findViewById(R.id.edt_shopprice);
edit_shopnum = (EditText) findViewById(R.id.edit_shopnum);
Button bt5 = (Button) findViewById(R.id.button5);
bt5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ShopAddsActivity.this.finish();
}
});
button_addshop = (Button) findViewById(R.id.button_addshop);
button_addshop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String shopname = edit_shopname.getText().toString();
String shopintro = edit_shopintro.getText().toString();
int shopprice = Integer.parseInt(edit_shopprice.getText()
.toString());
int shopnum = Integer.parseInt(edit_shopnum.getText()
.toString());
String sql = “insert into goods values(‘” + shopname + “‘,'”
+ shopintro + “‘,”+shopprice + “,” + shopnum + “,'” + userId + “‘)”;
try {
// 打开数据库,如果数据库不存在则创建数据库
mSQLiteDatabase = openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopAddsActivity.this, “添加商品成功!”,
1000).show();
Intent i = new Intent(ShopAddsActivity.this,
null);//SellerCheckShopActivity.class
Bundle b = new Bundle();
b.putString(“user”, userId);
i.putExtra(“data”, b);
startActivity(i);
} catch (Exception e) {
// Toast.makeText(ShopAddsActivity.this,
// “添加商品异常!” + e.getMessage(), 1000).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.seller_add_shop, menu);
return true;
}
}
已购商品类:
package com.example.shopcenter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class ShopbuyerActivity extends Activity {
private SQLiteDatabase mSQLiteDatabase = null;
// 数据库名称
private static final String DATABASE_NAME = “test.db”;
private String userId, shopname;
private TextView tv;
private ListView lv1, lv2;
int buyermoney;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buyer_check_shop);
Intent i = this.getIntent(); // 接收数据
Bundle b = i.getBundleExtra(“data”);
userId = b.getString(“user”);
lv2 = (ListView) this.findViewById(R.id.lv_buyer_money);
lv1 = (ListView) this.findViewById(R.id.lv_buyer_myshop);
mSQLiteDatabase = openOrCreateDatabase(DATABASE_NAME, Activity.MODE_PRIVATE, null);
selectBuyermoney();
selectData();
Button bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ShopbuyerActivity.this.finish();
}
});
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// 根据参数arg2获取当前位置的数据(行),并根据key获取对应的value
@SuppressWarnings(“unchecked”)
Map<String, Object> map = (Map<String, Object>) lv1.getItemAtPosition(arg2);
tv.setText(map.get(“shopname”).toString());
shopname = map.get(“shopname”).toString();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.buyer_check_shop, menu);
return true;
}
private void selectData() {
String sql = “select g.shopname,g.shopprice,bd.buyshopnum,g.shopseller from buyer_goods as bd,goods as g where bd.buyerid='”
+ userId + “‘ and bd.shopname=g.shopname”;
Cursor cursor = mSQLiteDatabase.rawQuery(sql, null);
if (cursor != null) {
// 定义存储查询结果的List,每一行数据类型为Map<String, Object>,其中String代表列名,Object为值
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
// 遍历游标对象,每一行获取的数据存放到Map<String, Object>类型的变量map中
while (cursor.moveToNext()) {
map = new HashMap<String, Object>();
map.put(“shopname”, cursor.getString(cursor.getColumnIndex(“shopname”)));
map.put(“shopprice”, cursor.getInt(cursor.getColumnIndex(“shopprice”)));
map.put(“buyshopnum”, cursor.getInt(cursor.getColumnIndex(“buyshopnum”)));
map.put(“shopseller”, cursor.getString(cursor.getColumnIndex(“shopseller”)));
list.add(map);
}
// 创建Adapter,并于ListView绑定
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.buyer_myshop,
new String[] { “shopname”, “shopprice”, “buyshopnum”, “shopseller” },
new int[] { R.id.tv_buyer_myshopname, R.id.tv_buyer_myshopprice, R.id.tv_buyer_myshopnum,
R.id.tv_buyer_mysellername });
lv1.setAdapter(adapter);
}
}
private void selectBuyermoney() {
String sql = “select usermoney from user where userid='” + userId + “‘”;
// 获得查询结果对应的游标对象
Cursor cursor = mSQLiteDatabase.rawQuery(sql, null);
if (cursor != null) {
// 定义存储查询结果的List,每一行数据类型为Map<String, Object>,其中String代表列名,Object为值
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
while (cursor.moveToNext()) {
map = new HashMap<String, Object>();
map.put(“usermoney”, cursor.getInt(cursor.getColumnIndex(“usermoney”)));
list.add(map);
}
// 创建Adapter,并于ListView绑定
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.buyer_money2, new String[] { “usermoney” },
new int[] { R.id.tv_buyer_money2 });
lv2.setAdapter(adapter);
}
}
}
商品购买类:
package com.example.shopcenter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
@SuppressLint(“ShowToast”)
public class ShopbSelectActivity extends Activity {
private SQLiteDatabase mSQLiteDatabase = null;
//数据库名称
private static final String DATABASE_NAME = “test.db”;
private TextView tv;
private ListView lv1,lv2;
private Button btn_selectshop,btn_myshop,btn_addmoney;
private String userId;
private String shopprice;
EditText edt_addmoney,edt_buy_shopnum;
int buy_shopnum;
int buyermoney;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buyer_select_shop);
Intent i = this.getIntent();
Bundle b = i.getBundleExtra(“data”);
userId = b.getString(“user”);
tv = (TextView)this.findViewById(R.id.tv_buyer_selectshopname);
lv2 = (ListView) this.findViewById(R.id.lv_buyer_money);
lv1 = (ListView) this.findViewById(R.id.lv_buyer_shop);
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
@SuppressWarnings(“unchecked”)
Map<String, Object> map = (Map<String, Object>) lv1
.getItemAtPosition(arg2);
tv.setText(map.get(“shopname”).toString());
shopprice=map.get(“shopprice”).toString();
}
});
try {
mSQLiteDatabase = this.openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
} catch (Exception ex) {
Toast.makeText(this, “打开或创建数据库异常:” + ex.getMessage(), 2000).show();
}
selectData();
selectBuyermoney();
edt_buy_shopnum=(EditText)this.findViewById(R.id.edittext_buy_shopnum);
btn_selectshop = (Button)this.findViewById(R.id.button_buyer_selectshop);
btn_selectshop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String shopname = tv.getText().toString();
buy_shopnum=Integer.parseInt(edt_buy_shopnum.getText().toString());
int price=Integer.parseInt(shopprice);
int total_price=buy_shopnum*price;
try {
String a=”select usermoney from user where userid='”+userId+”‘ ” ;
Cursor cursor = mSQLiteDatabase.rawQuery(a, null);
while (cursor.moveToNext()) {
buyermoney= cursor.getInt(cursor.getColumnIndex(“usermoney”));
}
if(buyermoney>=total_price){
String sql = “update goods set shopnum=shopnum-“+buy_shopnum+” where shopname='”+shopname+”‘ ” ;
mSQLiteDatabase.execSQL(sql);
sql=”update user set usermoney=usermoney-“+total_price+” where userid='”+userId+”‘”;
mSQLiteDatabase.execSQL(sql);
sql = “insert into buyer_goods(buyerid,shopname,buyshopnum) values(‘” + userId + “‘,'”
+ shopname + “‘,'”+buy_shopnum+”‘)”;
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopbSelectActivity.this, “购买成功!”,
1000).show();
Intent i = new Intent(ShopbSelectActivity.this,
ShopbSelectActivity.class);
Bundle b = new Bundle();
b.putString(“user”, userId);
i.putExtra(“data”, b);
startActivity(i);
}
else {Toast.makeText(ShopbSelectActivity.this, “余额不足,请充值!”,
3000).show();}
} catch (Exception e) {
Toast.makeText(ShopbSelectActivity.this,
“添加商品异常!” + e.getMessage(), 1000).show();
}
}
});
btn_myshop= (Button)this.findViewById(R.id.button_buyer_myshop);
btn_myshop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(ShopbSelectActivity.this,
ShopbuyerActivity.class);
Bundle b = new Bundle(); //发送数据
b.putString(“user”, userId);
i.putExtra(“data”, b);
startActivity(i);
}
});
edt_addmoney=(EditText)this.findViewById(R.id.edittext_addmoney);
btn_addmoney= (Button)this.findViewById(R.id.button_addmoney);
btn_addmoney.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String addmoney=edt_addmoney.getText().toString();
String sql = “update user set usermoney=usermoney+”+addmoney+” where userid='”+userId+”‘ ” ;
mSQLiteDatabase.execSQL(sql);
Toast.makeText(ShopbSelectActivity.this, “成功充值“+addmoney+”元!”,
1000).show();
Intent i = new Intent(ShopbSelectActivity.this,
ShopbSelectActivity.class);
Bundle b = new Bundle();
b.putString(“user”, userId);
i.putExtra(“data”, b);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.buyer_select_shop, menu);
return true;
}
private void selectData() {
String sql = “select * from goods “;
//获得查询结果对应的游标对象
Cursor cursor = mSQLiteDatabase.rawQuery(sql, null);
if (cursor != null) {
//定义存储查询结果的List,每一行数据类型为Map<String, Object>,其中String代表列名,Object为值
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
//遍历游标对象,每一行获取的数据存放到Map<String, Object>类型的变量map中
while (cursor.moveToNext()) {
map = new HashMap<String, Object>();
map.put(“shopname”, cursor.getString(cursor.getColumnIndex(“shopname”)));
map.put(“shopintro”, cursor.getString(cursor.getColumnIndex(“shopintro”)));
map.put(“shopprice”, cursor.getInt(cursor.getColumnIndex(“shopprice”)));
map.put(“shopnum”, cursor.getString(cursor.getColumnIndex(“shopnum”)));
map.put(“shopseller”, cursor.getString(cursor.getColumnIndex(“shopseller”)));
list.add(map);
}
//创建Adapter,并于ListView绑定
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.buyer_shop, new String[] { “shopname”,”shopintro”, “shopprice”,
“shopnum”,”shopseller”}, new int[] { R.id.tv_buyer_myshopname,R.id.tv_buyer_myshopintro, R.id.tv_buyer_myshopprice,
R.id.tv_buyer_shopnum ,R.id.tv_buyer_sellername});
lv1.setAdapter(adapter);
}
}
private void selectBuyermoney(){
String sql = “select usermoney from user where userid='”+userId+”‘”;
//获得查询结果对应的游标对象
Cursor cursor = mSQLiteDatabase.rawQuery(sql, null);
if (cursor != null) {
//定义存储查询结果的List,每一行数据类型为Map<String, Object>,其中String代表列名,Object为值
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map;
while (cursor.moveToNext()) {
map = new HashMap<String, Object>();
map.put(“usermoney”, cursor.getInt(cursor.getColumnIndex(“usermoney”)));
list.add(map);
}
//创建Adapter,并于ListView绑定
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.buyer_money, new String[] { “usermoney”}, new int[] { R.id.tv_buyer_money});
lv2.setAdapter(adapter);
}
}
}
最新评论