(功能:扫描商品的条形码,记录价格,再次扫描此码时可查询价格,也可以修改价格,删除记录)
下载zxing项目源代码 点击免费下载 导入里面的ZXingDemo工程
在包com.test.zxingdemo下新建一个DatabaseHelper类,继承自SQLiteOpenHelper,用于存储ID,PRICE,源码如下
package com.test.zxingdemo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME="goodsdata.db";//数据库名称,可自行定义
private static final int version=1;//版本号,用于数据库的更新
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql="create table goods(ID int not null,PRICE float(2) not null);";//创建数据库goods
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
接着再写一个简单的界面,代码如下
package com.test.zxingdemo;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Nickname;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.zxing.activity.CaptureActivity;
public class MainActivity extends Activity {
private String scanResult;
private long id;
private DatabaseHelper databaseHelper;
private SQLiteDatabase db;
private Button query, scanning, add, delete, update;
private TextView result_id;
private EditText result_price;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
databaseHelper = new DatabaseHelper(this);
db = databaseHelper.getWritableDatabase();
add = (Button) findViewById(R.id.add);
delete = (Button) findViewById(R.id.delete);
update = (Button) findViewById(R.id.update);
scanning = (Button) findViewById(R.id.btn_scanning);
query = (Button) findViewById(R.id.query);
query.setOnClickListener(listener);
scanning.setOnClickListener(listener);
add.setOnClickListener(listener);
delete.setOnClickListener(listener);
update.setOnClickListener(listener);
result_price = (EditText) findViewById(R.id.result_price);
result_id = (TextView) findViewById(R.id.result_id);
}
/**
* 点击事件处理
*/
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_scanning:
scanning();
break;
case R.id.add:
// String string = result_price.getText().toString();
// double set_price = Double.parseDouble(string);
add();
break;
case R.id.update:
update();
break;
case R.id.delete:
delete();
break;
case R.id.query:
query();
break;
}
}
};
private void add() {
// 考虑异常情况
if (result_price.getText().length() == 0) {
Toast.makeText(this, "请输入金额", Toast.LENGTH_SHORT).show();
} else {
String string = result_price.getText().toString();
double set_price = Double.parseDouble(string);
String sql = "insert into goods values(+" + id + "," + set_price
+ ")";
db.execSQL(sql);
Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
}
}
private void query() {
double price = 0;
Cursor c = db.rawQuery("select PRICE from goods where ID=?",
new String[] { id + "" });
while (c.moveToNext()) {
price = c.getDouble(c.getColumnIndex("PRICE"));
}
//考虑异常情况
if (price == 0) {
Toast.makeText(this, "无记录", Toast.LENGTH_SHORT).show();
} else {
result_price.setText(price + "");
}
}
private void delete() {
String sql = "delete from goods where ID=" + id;
db.execSQL(sql);
result_id.setText("");
result_price.setText("");
Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
}
private void update() {
//考虑异常情况
if (result_price.getText().length() == 0) {
Toast.makeText(this, "请输入金额", Toast.LENGTH_SHORT).show();
}
else {
String string = result_price.getText().toString();
double set_price = Double.parseDouble(string);
String sql = "update goods set price=" + set_price;
db.execSQL(sql);
Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
}
}
private void scanning() {
Intent intent = new Intent(this, CaptureActivity.class); // CaptureActivity是扫描的Activity类
startActivityForResult(intent, 0); // 当前扫描完条码或二维码后,会回调当前类的onActivityResult方法,
}
/**
* 重写onActivityResult 方法 当前对象若以startActivityForResult方式转跳页面,当目标页面结束以后,会回调此方法
*
* @param requestCode
* 该参数就是 startActivityForResult(intent, 0); 中的第二个参数值
* @param resultCode
* 是转跳的目标页面中,setResult(RESULT_OK, intent); 中的第一个参数值
* @param data
* 转跳的Intent对象,可以传递数值
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) { // 判断回调
Bundle bundle = data.getExtras();
scanResult = bundle.getString("result"); // 这就获取了扫描的内容了
result_id.setText(scanResult);
id = Long.parseLong(scanResult);
}
}
}
补充说明:这只是在原项目上进行修改,如果想将其应用到自己的项目中,可按照以下步骤
1.将layout文件夹下的文件复制到你项目的layout目录下
2.将raw文件夹复制到你res目录下
3.将values文件夹下的文件复制到你项目的res/values目录下
4.将core.jar复制到你项目的libs目录下
5.将com文件夹复制到你的src目录下,修改一下R文件的引用(就是有些页面引用了R文件,包名要改成你的包名,如果你是用Eclipse 或者是myEclipse的话,有个快捷键 Ctrl + Shift + O 就可以快速帮你引用R文件了)
6.到AndroidManifest.xml文件中,声明一个activity 例如:
7.在AndroidManifest.xml文件中增加以下权限