我想创建android条码扫描器并显示SQL数据库的查询结果。可以使用视图按钮检索结果:
btnview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if((edtproname.getText().toString().length()==28)&&(edtproname.getText() !=null))
try {
PreparedStatement statement = connect.prepareStatement("EXEC viewProduct '"+edtproname.getText().toString()+"'");
String location = edtproname.getText().subSequence(20,28).toString();
edtlocation.setText(location);
rs = statement.executeQuery();
while (rs.next()) {
edtproname.setText(rs.getString("ProName"));
edtprodesc.setText(rs.getString("ProDesc"));
}
} catch (SQLException e) {
Toast.makeText(AddProducts.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
edtqty.requestFocus();
InputMethodManager manager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(edtqty, InputMethodManager.SHOW_FORCED);
}
这是条形码读取事件:
@Override
public void onBarcodeEvent(final BarcodeReadEvent event){
runOnUiThread(new Runnable() {
@Override
public void run() {
String barcodeData = event.getBarcodeData();
//String timestamp = event.getTimestamp();
edtproname.setText(""+barcodeData);
}
加载扫描仪结果后如何完成自动显示?没有查看按钮?如何直接使用硬件触发器显示结果?
我感谢任何帮助...... 谢谢。
图片:sample app
答案 0 :(得分:0)
任何事件调用都像触发器一样。因此,只需使用onBarcodeEvent()中的run()函数来触发搜索。只需在获取barcodeData字符串后输入查询代码即可。
public void onBarcodeEvent(final BarcodeReadEvent event){
runOnUiThread(new Runnable() {
@Override
public void run() {
String barcodeData = event.getBarcodeData();
edtproname.setText(""+barcodeData);
if(barcodeData.length()==28){
try {
PreparedStatement statement = connect.prepareStatement("EXEC viewProduct '"+barcodeData+"'");
String location = edtproname.getText().subSequence(20,28).toString();
edtlocation.setText(location);
rs = statement.executeQuery();
while (rs.next()) {
edtproname.setText(rs.getString("ProName"));
edtprodesc.setText(rs.getString("ProDesc"));
}
} catch (SQLException e) {
Toast.makeText(AddProducts.this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
edtqty.requestFocus();
InputMethodManager manager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(edtqty, InputMethodManager.SHOW_FORCED);
}
}
});
}
然后删除视图按钮。