so, i've been working on this same stupid thing for a while now. some folks here have helped me get it to the point it is but now i've got to move farther forward... but first, my code:
package com.mhe.test.scan;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myScanButton = (Button) findViewById(R.id.myScanButton);
totalbox = (EditText) findViewById(R.id.tBox);
myScanButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
}
});
}
private EditText totalbox;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
final String contents = intent.getStringExtra("SCAN_RESULT");
if ( totalbox != null );
totalbox.setText(contents);
Context context = getApplicationContext();
CharSequence text = "Successful Scan";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Button myTotalButton = (Button) findViewById(R.id.myTotalButton);
myTotalButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
Intent pass = new Intent(view.getContext(), Result.class);
startActivityForResult(pass, 0);
}
});
开发者_运维问答} else if (resultCode == RESULT_CANCELED) {
if ( totalbox != null );
totalbox.setText("bummer");
}
}
}
}
so anyhow, what i'd like to happen is upon a successful scan, the result is loaded into the EditText totalbox. then the 'myTotalButton' is clicked and will pass the result to the next activity 'Result.class'. right now i'm just trying to get it to switch to the new activity. If the
Button myTotalButton = (Button) findViewById(R.id.myTotalButton);
myTotalButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
Intent pass = new Intent(view.getContext(), Result.class);
startActivityForResult(pass, 0);
code is there, it FCs. otherwise, the rest of it works fine. any suggestions/assistance would be helpful. I feel like i am missing something stupid that i will smack myself for.
Have you added the new activity to your Manifest? Also, have you included the intent integrator code from the zxing project?
I think I just realized what the problem is.
The button press activity calls back into the same onActivityResult
method it's called from, and using the same requestCode of 0. This is causing the errors where you attempt to getStringExtra("SCAN_RESULT")
because that only exists in the callback from the scan.
Instead of startActivityForResult(pass, 0);
use startActivityForResult(pass, 1);
(or whatever digit) and handle it by adding an
} else if (1 == requestCode) {
\* your stuff to handle the button result here*\
}
code section to the existing requestCode if statement (or make it a switch() statement).
The end result would look something like this:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
final String contents = intent.getStringExtra("SCAN_RESULT");
if ( totalbox != null ) {
totalbox.setText(contents);
}
Toast toast = Toast.makeText(getApplicationContext(),
"Successful Scan", Toast.LENGTH_SHORT).show();
Button myTotalButton = (Button) findViewById(R.id.myTotalButton);
myTotalButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
Intent pass = new Intent(view.getContext(), Result.class);
startActivityForResult(pass, 1);
}
});
} else if (resultCode == RESULT_CANCELED) {
if ( totalbox != null ) {
totalbox.setText("bummer");
}
}
} else if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// Do whatever it is you want to do with the returned
// data from the Result.class activity call
}
}
}
So, am I correct in thinking that you just want to pass a result on to another activity? If so, there is an easy Java way of doing it that is non Android specific. If so, then let me know and I'll help.
精彩评论