0

I'm new to Java and this may be a basic question. It takes me long time to understand and hopefully someone will it to me. Why can't I put priceMessage at putExtra(Intent.EXTRA_TEXT ,....) ? Is there any possible way?

public String submitOrder (String name, int price, boolean addWhippedCream, boolean addChoc) {
    String priceMessage = getString(R.string.price_symbol) + (calculatePrice(addWhippedCream, addChoc));
    priceMessage += "\n" + getString(R.string.name_order) + name;
    priceMessage += "\n" + getString(R.string.add_whipped_cream) + " (" + addWhippedCream +")";
    priceMessage += "\n" + getString(R.string.add_chocolate) + " (" + addChoc + ")";
    priceMessage += "\n" + getString(R.string.cup_of_coffee) + " : " + quantity + " " + getString(R.string.cup_of_coffee);
    priceMessage += "\n" + getString(R.string.thank_you);
    displayMessage(priceMessage);
    return priceMessage;
}
/**
 * Send Button Order for Intent Action
 */
public void sendOrder (View view){
    EditText nameField = (EditText)findViewById(R.id. name_field);
    String name = nameField.getText().toString();
    Intent sendOrder = new Intent(Intent.ACTION_SENDTO);
    sendOrder.setData(Uri.parse("mailto:"));
    sendOrder.putExtra(Intent.EXTRA_SUBJECT,"Tempahan Kopi : " + name);
    sendOrder.putExtra(Intent.EXTRA_TEXT,priceMessage);
    startActivity(Intent.createChooser(sendOrder,"Hantar Tempahan"));
Paul Roub
  • 36,322
  • 27
  • 84
  • 93

1 Answers1

1

In Java (and many other Object-Oriented programming languages), variables defined in a method are only available inside that method. If you want to share variables between two different methods, you can pass them as method parameters, or create them as either static or instance class members.

I'd recommend checking out the official Java Trails Tutorials to help you get started with the language. It'll explain what the different types of variables are, and when you might want to use them.

Andrew Rueckert
  • 4,858
  • 1
  • 33
  • 44