Reputation: 43
I seen a lot of questions very similar to mine but nothing seems to be working, so I apologize if this has been answered before.
So what I have is a app with a calculator showing how much money you can save when you roll your own smokes. In the calculator I have a series of edit text boxes for entering: Amount your currently paying for a pack of cigarettes, tax for your state, smokes per day, cost for bag of tobacco, and cost for tubes. Upon hitting calculate it will tell you how much you are currently paying for premade cigarettes vs rolling your own.
The problem that lies is there are many different size bags of tobacco so for everything to work out it has to based off of 6 ounce bags. So what I developed is a worksheet to figure out how much you would pay for the right amount. For instance, if I buy a 16oz bag the calculation wouldn't come out right, so the worksheet will tell you how much you'd pay for a 6oz bag of the same product.
So what I can't figure out is how to take that new price from my worksheet and place it in the calculator activity. From what I read the getText and setText doesn't work between activities. I also seen the intent method where you putExtras from current activity and getExtras on the activity you want it to go to. But every example I've seen involves starting a new activity. When I simply want to take and basically copy the number in worksheet, close the worksheet, and place it in my calculator activity.
I'd greatly appreciate any info you can give me, because I've been searching for this answer for literally months. Ok, I gave up for a while now I'm back on it. If you need me to post code or any other info just let me know.
This is my calculator. This is where I want to put the new number in editText(perbag)
public class Calculator extends Activity {
EditText perbag
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.calculator);
final MediaPlayer lighterFlick = MediaPlayer.create(this,
R.raw.lighter_sound);
// worksheet clickable textview
TextView initiateWorksheet = (TextView) findViewById(R.id.initiateWorksheet);
initiateWorksheet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.trhodes.saveonsmokes.WORKSHEET"));
}
});
This is the worksheet activity which is opened from within the calculator activity. So the first part of this is just getting the number I need in "wsnewprice". So at the bottom I have the button "wsok" which is where I want to take "wsnewprice" of the worksheet, then close out work sheet and place it in the editText "perbag" of the above calculator activity.
public class Worksheet extends Activity{
EditText wspriceperbag = null;
EditText wsweight = null;
EditText wsnewprice = null;
Button wscalculate;
Button wsok;
double Wspriceperbag = 0.00D;
double Wsweight = 0.00D;
double Wsnewprice = 0.00D;
double ounce = 6;
public Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.worksheet);
wspriceperbag = (EditText) findViewById(R.id.wspriceperbag);
wsweight = (EditText) findViewById(R.id.wsweight);
wsnewprice = (EditText) findViewById(R.id.wsnewprice);
wscalculate = (Button) findViewById(R.id.wscalculate);
wsok = (Button) findViewById(R.id.wsok);
wscalculate.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
try {
Wspriceperbag = Double.parseDouble(wspriceperbag.getText()
.toString());
Wsweight = Double.parseDouble(wsweight.getText()
.toString());
} catch (NumberFormatException nfe) {
}
DecimalFormat localDecimalFormat = new DecimalFormat("#0.00");
Wsnewprice = (Wspriceperbag / Wsweight) * ounce;
wsnewprice.setText(localDecimalFormat.format(Wsnewprice));
}
});
wsok.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Now this was the code I was trying to use but like I said I don't want to start a new activity I just want to go to the previous one.
to:
perbag.setText(getIntent().getExtras().getString("newprice"));
from:
Intent intent = new Intent(Worksheet.this, Calculator.class);
intent.putExtra("newprice",wsnewprice.getText().toString());
startActivity(intent);
I'm sorry if this is confusing, I'm trying to be as clear as possible.
Edit: for the solution I found
new code for original activity
//this is to start the worksheet
initiateWorksheet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Calculator.this, Worksheet.class);
startActivityForResult(i, 0);
//this is coming back to the original activity and placing the new data..
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK);
Bundle bundle = data.getExtras();
String s = bundle.getString("newprice");
perbag.setText(s);
new code for the worksheet activity:
//this is getting the new number and putting it into a string
wsok.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newNumber = new Intent();
Bundle bundle = new Bundle();
bundle.putString("newprice", wsnewprice.getText().toString());
newNumber.putExtras(bundle);
setResult(RESULT_OK, newNumber);
finish();
//had to add this because force close when hitting the back button
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent newNumber = new Intent();
Bundle bundle = new Bundle();
bundle.putString("newprice", "");
newNumber.putExtras(bundle);
setResult(RESULT_OK, newNumber);
finish();
Thanks to everyone you responded, without you guys I'd still be pulling my hair out. Plus I'm all ears if anyone has a suggestion on the force closing on hitting the back button with out the last lines of code. Otherwise is cool cause it's actually working the way I want.
thanks, Travis
Upvotes: 1
Views: 1974
Reputation: 4070
Not sure whether I got your question right, but passing data back from an activity is similar to passing data to an activity. You would probably start your worksheet with startActivityForResult
and then take a look into the returned result data after returning in onActivityResult
, which is wrapped into an Intent
, too.
Here's an example for emphasis.
public class CalculatorActivity extends Activity {
/** Some locally unique identifier to recognize where you're coming from. */
private static final int RECQ_WORKSHEET = 1;
/**
* Some event that triggers your worksheet activity.
*/
public void onWorksheetButtonTouched( final View view ) {
Intent intent = new Intent( this, WorksheetActivity.class );
startActivityForResult( intent );
}
/**
* Method which is called when returning from an activity
* called with startActivityForResult.
*/
@Override
public void onActivityResult( int requestCode, int result, Intent data ) {
if( requestCode == REQC_WORKSHEET ) {
// We're coming back from the worksheet here, so fetch the returned data.
final String numberFromWorksheet = data.getStringExtra( WorksheetActivity.EXTRA_NUMBER );
...
}
...
}
}
Your worksheet activity would return its data wrapped into an Intent
and store it with setResult
.
public class WorksheetActivity extends Activity {
/**
* Having accessible extra parameters enumerated in some way
* makes your life (and the life of your colleagues) easier.
* I've seen people adding _IN_, _OUT_ or _IO_ to emphasize
* whether it's an input or output parameter, or both.
*/
public static final String EXTRA_NUMBER = "number";
...
/**
* Some event which closes the worksheet activity.
*/
public void returnFromWorksheetButtonTouched( final View view ) {
// Fetch the text you want to return.
final EditText someText = (EditText) findViewById( R.id.someIdOfYourEditText );
final String value = someText.getText();
// Put it into an Intent.
final Intent resultData = new Intent(); // note that we're using the no-args c'tor here
resultData.putExtra( EXTRA_NUMBER, value );
// Now finish with this result.
setResult( RESULT_OK, resultData );
finish();
}
}
Upvotes: 1
Reputation: 22637
are you asking how to pass data between activities? to start a new activity, you probably created an Intent
and started it like,
Intent i = new Intent(this, OtherActivity.class);
startActivity(i);
if you want to pass data to the other activity, just add it to the intent,
Intent i = new Intent(this, OtherActivity.class);
i.putExtra("foo", "bar");
startActivity(i);
in the receiving activity,
Intent i = getIntent(); // get the intent that started me
String foo = i.getStringExtra("foo");
Upvotes: 3