一个简单的商品管理系统

 


 

 

该商品管理系统开发的目的在于学习。实现的功能都比较简单,只有根据界面的信息进行相应的操作就可以了,目前只做了添加的功能。
声明一个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;
 }

 

布局这里用的是线性布局,但在实际开发中,一个好的布局是比较重要的都比较简单。所以废话我也不多说,直接看我是怎么样布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
 <TextView 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="商品编号:"
     android:textSize="18dip"/>
 <EditText android:id="@+id/id"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>
  </LinearLayout>
 <LinearLayout android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">
  <TextView android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="商品名称:"
      android:textSize="18dip"/>
  <EditText android:id="@+id/name"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"/>
 </LinearLayout>
 <LinearLayout android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">
  <TextView android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="商品类别:"
      android:textSize="18dip"/>
  <RadioGroup  android:orientation="horizontal"
   android:id="@+id/sort"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   <RadioButton android:id="@+id/fuzhang"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="服装"
    android:textSize="18dip"/>
   <RadioButton android:id="@+id/frimily"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="家电"
    android:textSize="18dip"/>
   <RadioButton android:id="@+id/baihuo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="百货"
    android:textSize="18dip"/>
   </RadioGroup>
 </LinearLayout>
 <LinearLayout android:orientation="horizontal"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content">
  <TextView android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="商品数量:"
      android:textSize="18dip"/>
  <EditText android:id="@+id/number"
    android:phoneNumber="true"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"/>
 </LinearLayout>
 <Button android:id="@+id/button"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="添加商品"/>
</LinearLayout>

 

以上是我做的一个简单的android手机商品管理系统,目前值做了添加的功能,当然这里面有很多代码都还可以在优化,比如用来管理商品信息的数据库类可以整合到一个类,把内部类去掉。至于要看大家是怎么使用了。

 

 

 

 

 

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值