0

我想创建 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);
}

如何在扫描仪结果加载后自动显示?没有查看按钮?如何直接使用硬件触发显示结果?

感谢您的帮助...谢谢。

图片:示例应用程序

4

1 回答 1

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);

            }
        }
    });
}

并删除查看按钮。

于 2017-12-12T04:08:55.460 回答