该商品管理系统开发的目的在于学习。实现的功能都比较简单,只有根据界面的信息进行相应的操作就可以了,目前只做了添加的功能。
声明一个GoodsSystem类(Activity)用来显示界面的信息
/*
* 界面上的相关按钮
*/
Button add;
EditText id;
EditText name;
EditText number;
RadioGroup sort;
RadioButton fuzhuang;
RadioButton jiadian;
RadioButton baihuo;
LibraryDapter goods;//SQLite数据库
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
goods=new LibraryDapter(this);
add=(Button)findViewById(R.id.button);
id=(EditText)findViewById(R.id.id);
name=(EditText)findViewById(R.id.name);
number=(EditText)findViewById(R.id.number);
/*对事件进行监听*/
add.setOnClickListener(this);
sort=(RadioGroup)findViewById(R.id.sort);
fuzhuang=(RadioButton)findViewById(R.id.fuzhang);
jiadian=(RadioButton)findViewById(R.id.frimily);
baihuo=(RadioButton)findViewById(R.id.baihuo);
}
@Override
public void onClick(View v) {
String str="";
/*如果触发事件,则执行if语句里面的相应信息*/
if(v==add){
if(fuzhuang.isChecked()){
str="服装";
}
if(jiadian.isChecked()){
str="家电";
}
if(baihuo.isChecked()){
str="百货";
}
try {
/*得到商品编号、商品名称、商品类别、商品数量;然后添加都数据库里面,添加成功后返回一个提示信息*/
goods.goods_ID = id.getText().toString();
goods.goods_Name = name.getText().toString();
goods.goods_Short = str;
goods.goods_Number = Integer.parseInt(number.getText().toString());
goods.insert();
Toast.makeText(this, "添加成功!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "请正确填写添加的信息!", Toast.LENGTH_LONG).show();
}
}
}
出了要声明一Activity用来显示界面信息之外,还要创建一个类,用来保存商品信息。这里我是先声明了一个LibraryDapter类,然后在这个类里面再创建一个内部类,该类继承了
SQLiteOpenHelper,SQLiteOpenHelper和SQLiteDatabase是用来处理数据库创建、存储等信息的。首先要定义数据库的名称、表的名称、表的字段名称(商品编号、商品名称、商品类别、商品
数量),具体看下面的代码:
private static class DBOpenHelper extends SQLiteOpenHelper {
private static final String DB_Create = "create table " + goods_table
+ " (" + goodsID + " text not null," + goodsName
+ " text not null," + goodsSort + " text not null,"
+ goodsNumber + " integer);";
public DBOpenHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
_db.execSQL("DROP TABLE IF EXISTS " + goods_table);
onCreate(_db);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_Create);
}
}
这里是LibraryDapter里面的一些方法,主要是进行创建数据库,向数据库添加商品的方法。
public LibraryDapter(Context _context) {
context = _context;
open();
}
/*创建数据库*/
public void open() {
dbOpenHelper = new DBOpenHelper(context, goods_name, null, goods_versiow);
try {
dbsql = dbOpenHelper.getWritableDatabase();
} catch (SQLiteException e) {
dbsql = dbOpenHelper.getReadableDatabase();
}
}
/*添加商品*/
public long insert() {
ContentValues newValues = new ContentValues();
newValues.put(goodsID, this.goods_ID);
newValues.put(goodsName, this.goods_Name);
newValues.put(goodsSort, this.goods_Short);
newValues.put(goodsNumber, this.goods_Number);
return dbsql.insert(goods_table, null, newValues);
}
public String toString() {
String request = "";
request = "商品编号:" + this.goods_ID + ",";
request = "商品名称:" + this.goods_Name + ",";
request = "商品类别:" + this.goods_Short + ",";
request = "商品数量:" + this.goods_Number + ",";
return request;
}
布局这里用的是线性布局,但在实际开发中,一个好的布局是比较重要的都比较简单。所以废话我也不多说,直接看我是怎么样布局
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_height="wrap_content"
>
android:layout_height="wrap_content"
android:text="商品编号:"
android:textSize="18dip"/>
android:layout_height="wrap_content"/>
android:layout_height="wrap_content">
android:text="商品名称:"
android:textSize="18dip"/>
android:layout_height="wrap_content"/>
android:layout_height="wrap_content">
android:text="商品类别:"
android:textSize="18dip"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:text="服装"
android:textSize="18dip"/>
android:layout_height="wrap_content"
android:checked="true"
android:text="家电"
android:textSize="18dip"/>
android:layout_height="wrap_content"
android:text="百货"
android:textSize="18dip"/>
android:layout_height="wrap_content">
android:text="商品数量:"
android:textSize="18dip"/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
以上是我做的一个简单的android手机商品管理系统,目前值做了添加的功能,当然这里面有很多代码都还可以在优化,比如用来管理商品信息的数据库类可以整合到一个类,把内部类去掉。至于要看大家是怎么使用了。