我有两个扫描事件。
第一个,我的MainActivity
中的QR码扫描
代码:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.scanbutton);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent,10);
}
});
而secen one,我AddDataFragment
中的简单条形码扫描
代码:
ImageButton ib = (ImageButton) view.findViewById(R.id.button_scan_serial);
ib.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR,EAN_13,EAN_8,UPC_A");
startActivityForResult(intent,11);
}
});
这是onActivityResult
MainActivity
代码
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
String scanContent = intent.getStringExtra("SCAN_RESULT");
String scanFormat = intent.getStringExtra("SCAN_RESULT_FORMAT");
Log.d("INHALT", scanContent);
Log.d("FORMAT", scanFormat);
switch (requestCode){
case (10):{
String[] Split = scanContent.split("\\s");
String product = Split[0];
String label = Split[1];
String serial = Split[2];
String mac = Split[3];
String daaid = Split[4];
String bill = Split[5];
mydb.insertData(product, label, serial, mac, daaid, bill);
break;
}
case (11):{
Log.d("TEST", "test test test - arrived here");
}
}
} else if (resultCode == RESULT_CANCELED) {
Log.d("TEST", "result_canceled");
break;
}
Fragment fragment;
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fragment = new ListViewFragment();
ft.replace(R.id.container, fragment);
ft.commitAllowingStateLoss();
}
但如果我扫描条形码,则案例开关没有检测到10
。扫描zxing关闭后,弹出ListViewFragment
。有什么错吗?如何将scanContent
字符串推送到AddDataFragment
?
答案 0 :(得分:1)
你错过了休息;
switch (requestCode){
case (10):{
String[] Split = scanContent.split("\\s");
String product = Split[0];
String label = Split[1];
String serial = Split[2];
String mac = Split[3];
String daaid = Split[4];
String bill = Split[5];
mydb.insertData(product, label, serial, mac, daaid, bill);
break;
}
case (11):{
Log.d("TEST", "ICH HABE ERKANNT DAS ICH EINEN BARCODE GESCANNT HABE");
break;
}
}