I’m new to Android and trying to develop a simple android app. I’m trying to send two values from different activities as arguments to a third one. The problem is that the method that I’m using those two values, isn’t seeing the values that’s been entered by the user. Thanks in advance.
The code in that activity is:
public class NewAcount extends Activity {
private DecimalFormat twoDigits = new DecimalFormat("0.00");
TextView final_Amount;
EditText price, amount_Entered;
double userr_Entered_Amount, itemPricee, result;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.transparent);
// amount of money the user wants to spend daily
amount_Entered = (EditText) findViewById(R.id.et_Amount);
// item price
price = (EditText) findViewById(R.id.et_Price);
final_Amount = (TextView) findViewById(R.id.tv_Account_Result);
// set the text view value
final_Amount.setText(twoDigits.format(getTotal()));
}
public double getItemPrice(double itemPrice) {
this.itemPricee = itemPrice;
System.out.println("the item price in the new account activity is "
+ itemPricee);
return itemPricee;
}
public double getUserAmountEntered(double user_Entered_Amount) {
this.userr_Entered_Amount = user_Entered_Amount;
System.out.println("the user amount in the new account activity is "
+ userr_Entered_Amount);
return userr_Entered_Amount;
}
public double getTotal() {
// Here where the problem is. it's not seeing
// userr_Entered_Amount and itemPricee
// and returns result as zero
result=getUserAmountEntered(userr_Entered_Amount)-getItemPrice(itemPricee);
System.out.println("the result is " + result);
return result;
}
}
FinanceAppActivity.java
I’m sending the amount entered doing:
if (amount_Entered.getText().toString().length() > 0) {
// start your expenses app activity
Intent openYourExpensesActivity = new Intent(
"android.intent.action.FINANCESQLLITE");
startActivity(openYourExpensesActivity);
double user_Entered_Amount=Double.parseDouble(amount_Entered.getText().toString());
//send amount entered to new account activity
NewAcount na=new NewAcount();
na.getUserAmountEntered(user_Entered_Amount);
}
YourExpenses.java
sending item price
// to determine which button was clicked
switch (v.getId()) {
case R.id.btn_Add:
boolean didItWork = true;
try {
String itemName = item.getText().toString();
itemPrice = Double.parseDouble(price.getText().toString());
Price entry = new Price(YourExpenses.this);
entry.open();
// passing both strings to create entry method
entry.creatEntry(itemName, itemPrice);
entry.close();
NewAcount na=new NewAcount();
//send item price to new account activity
na.getItemPrice(itemPrice);
} catch (Exception e) {
//code here
} finally {
if (didItWork) {
Dialog d = new Dialog(this);
d.setTitle("Inserted successfully!");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent openNewAccount = new Intent("android.intent.action.TRANSPARENT");
startActivity(openNewAccount);
}
}
Are you just trying to call the public functions? This isn’t going to work because “typically” only one Activity has the foreground at a time. You can pass values with intents as extras, use a sharedpreference, of most extreme extend Application and use that to house all your data.