1

I'm trying to get my Android project to retrieve user numbers through the edit-text areas. That may already be fully completed in my code. I do not know how to use the captured data in an equation, and display the results back in the second activity on the screen through a textbox. The solution is probably more simple than it seems to be at the moment. Let me know if you want more specifics and information overall.

First activity:

 package com.klinetel.bac.calculator;

 import android.os.Bundle;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.view.View;
 import android.widget.*;
 import android.content.*;


 public class MainActivity extends Activity {

RadioGroup group1;
RadioButton maleRadio, femaleRadio;
 // Button button = (Button)findViewById(R.id.button1);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("The Result");
    alert.setMessage("Your Blood Alcohol Content Percentage is:");
    alert.setPositiveButton("OK", null);
    alert.create().show();


Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View view)
    {
        Intent intent = new Intent(view.getContext(),ResultsActivity.class);
        startActivityForResult(intent,0);
        finish();

    }

});
}



//Input conversion-to-string-attributes
public void calcBAC(){

   //weight
   EditText editWeight = (EditText)findViewById(R.id.editText1);
   double weight = Double.parseDouble(editWeight.getText().toString());

   //hours
   EditText editHours = (EditText)findViewById(R.id.editText2);
   double hours = Double.parseDouble(editHours.getText().toString()); 

   //drinks
   EditText editDrinks = (EditText)findViewById(R.id.editText3);
   double drinks = Double.parseDouble(editDrinks.getText().toString());

   //radio buttons
   maleRadio=(RadioButton)findViewById(R.id.radio0);

   femaleRadio=(RadioButton)findViewById(R.id.radio1);


   //bac total
   double resultBAC=0;

   //male formula
   if(maleRadio.isChecked()){

       resultBAC=(((EQUATION HERE))/2;

   }
   //female formula
   else if(femaleRadio.isChecked()){

       resultBAC=(((EQUATION HERE))/2;
   }

}


}

Second activity:

package com.klinetel.bac.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.*;
import android.widget.*;
import android.content.Intent;


public class ResultsActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results);


TextView result;

Button button = (Button)findViewById(R.id.backButton);

button.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View view)
    {
        Intent intent = new Intent(view.getContext(),MainActivity.class);
        startActivityForResult(intent,0);
        finish();

    }


});
result=(TextView)findViewById(R.id.textView1);
//result=resultBAC.getValue;
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Klinetel
  • 302
  • 1
  • 7
  • 27

4 Answers4

4

You can use Extra's to pass off data to the second activity. You can pass off single values to the intent or you can pass off a "Bundle" if you have a bunch of different data values to pass between activities

On your first activity add the following:

Intent intent = new Intent(view.getContext(),ResultsActivity.class);
intent.putExtra("extraname", "extravalue");
intent.putExtra("extraname2", 9.99);
startActivityForResult(intent,0);
finish();

Then on your receiving activity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_results);
String extra = getIntent().getStringExtra("extraname") //returns null if nothing
Double extra2 = getIntent().getDoubleExtra("extraname2")

Etc

ainesophaur
  • 774
  • 5
  • 12
  • I am rather new to Android, what are the "extranames" and "extravalues" mean? Also, when I use extraname2 in the getDoubleExtra, I get an error. Probably because this is sample data. After this, how would I display the equation answer in a textbox? Thanks – Klinetel Aug 22 '12 at 23:18
  • 1
    I forgot that getDoubleExtra requires two parameters.. key name and default value. Try getIntent().getDoubleExtra("extraname2", 0.00) extraname and extravalue in this case are just examples of how extras work. extraname is the extra "keyname" and extravalue is just the value of the string extra. On the activity doing the calculations you'll have global variables for weight, hours and drinks and use getDoubleExtra (or whatever type) to grab it during onStart..then when you do the calculation then reference the global variables. – ainesophaur Aug 23 '12 at 12:51
1

If you want to send data to next Activity, you can use Intent.putExtra() and Intent.getExtra() to send and retrieve data respectively between activities.

Community
  • 1
  • 1
Sayyam
  • 959
  • 10
  • 22
1

You can use the 'Bundle' class to share data between activities. You can do something like this for example:

Bundle extras = getIntent().getExtras();
if (extras != null) 
{
    username = extras.getString("username");
    password = extras.getString("password");
}
shazeline
  • 513
  • 1
  • 3
  • 7
0

..or you could simply implements an utility Class with static method to store the value you want to pass between activities.

FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22