So I have a very simple app I am working on. It's purpose is to collect asset data from 1 pc, and 1 or 2 monitors.
My form contains 3 edittext views, and 3 buttons (one for each asset I am collecting data for). The buttons invoke startActivityForResult for the barcode scanner, then I want to pass the result to the associated edittext view based on which button was pressed (example: press "scan" button to the right of "Asset - PC" edittext, scan and return data to it's associated edittext. Then if you press the button "scan" thats next to the "Asset - Mon1" edittext, return data to "Asset - Mon1" edittext.... and so on...)
With the code I have here, all the items work, just not as intended. Pressing any of the "scan" buttons always returns the result to the first edittext view "Asset - PC".
public class TestShit extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void assetPcClick(View view) {
Intent intent1 = new Intent("com.google.zxing.client.android.SCAN");
intent1.setPackage("com.google.zxing.client.android");
intent1.putExtra("SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent1, 0);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetPC = (EditText) findViewById(R.id.assetPC);
assetPC.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
public void assetMon1Click(View view) {
Intent intent2 = new Intent("com.google.zxing.client.android.SCAN");
intent2.setPackage("com.google.zxing.client.android");
intent2.putExtra("SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent2, 0);
}
public void onActivityResult2(int requestCode, int resultCode, Intent intent2) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents2 = intent2.getStringExtra("SCAN_RESULT");
String format2 = intent2.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon1 = (EditText) findViewById(R.id.assetMon1);
assetMon1.setText(contents2);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
public void assetMon2Click(View view) {
Intent intent3 = new Intent("com.google.zxing.client.android.SCAN");
intent3.setPackage("com.google.zxing.client.android");
intent3.putExtra("SCAN_MODE", "ONE_D_MODE");
startActivityForResult(intent3, 0);
}
public void onActivityResult3(int requestCode, int resultCode, Intent intent3) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents3 = intent3.getStringExtra("SCAN_RESULT");
String format3 = intent3.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon2 = (EditText) findViewById(R.id.assetMon2);
assetMon2.setText(contents3);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
Any suggestions on how I can better manage my multiple "ActivityForResult" and "onActivityResult" 's ?
My fix, thank you for all your help!
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetPC = (EditText) findViewById(R.id.assetPC);
assetPC.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon1 = (EditText) findViewById(R.id.assetMon1);
assetMon1.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
if (requestCode == 2) {
if (resultCode == RESULT_OK) {
String contents1 = intent.getStringExtra("SCAN_RESULT");
String format1 = intent.getStringExtra("SCAN_RESULT_FORMAT");
EditText assetMon2 = (EditText) findViewById(R.id.assetMon2);
assetMon2.setText(contents1);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…