Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Using Intents to pass data between Activities

  • Loading branch information...
commit f73bb0138c49d26a4e33ac1b8f0f506292c6bf6c 1 parent ad67f27
Mike Wolfson authored
View
38 src/com/andevcon/bootcamp/DetailActivity.java
@@ -11,7 +11,9 @@
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
+import android.widget.EditText;
import android.widget.ShareActionProvider;
+import android.widget.TextView;
import android.widget.Toast;
/**
@@ -19,8 +21,7 @@
*/
public class DetailActivity extends Activity {
private ShareActionProvider provider;
-
-
+
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -28,17 +29,40 @@ protected void onCreate(Bundle savedInstanceState) {
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setTitle(R.string.expense_detail);
+
+ Intent intent = getIntent();
+ String name = intent.getStringExtra("name");
+ int amount = intent.getIntExtra("amount", 0);
+
+ String expense = convertToUsd(name, amount);
+
+ TextView expenseTextView = (TextView) findViewById(R.id.detail_expense_title);
+ expenseTextView.setText(expense);
+ }
+
+ private String convertToUsd(String name, int amount) {
+ String usd = "$00.00";
+ if (amount != 0) {
+ Integer amountInt = Integer.valueOf(amount);
+ String amountString = amountInt.toString();
+ String dollars = amountString.substring(0, amountString.length() - 2);
+ String cents = amountString.substring(amountString.length() - 2);
+ usd = "$" + dollars + "." + cents;
+ }
+
+ String expenseUSD = name + " : " + usd;
+ return expenseUSD;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.detail_menu, menu);
-
- //Setup share functionality
+
+ // Setup share functionality
MenuItem item = menu.findItem(R.id.menu_share);
provider = (ShareActionProvider) item.getActionProvider();
setShareIntent();
-
+
return true;
}
@@ -80,7 +104,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
return false;
}
-
+
public void setShareIntent() {
String shareSubject = "Expense to share: ";
String shareText = "Expense info: ";
@@ -91,6 +115,6 @@ public void setShareIntent() {
intent.putExtra(Intent.EXTRA_TEXT, shareText);
if (provider != null) {
provider.setShareIntent(intent);
- }
+ }
}
}
View
8 src/com/andevcon/bootcamp/MainActivity.java
@@ -24,10 +24,16 @@ protected void onCreate(Bundle savedInstanceState) {
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = expenseName.getText().toString();
- String amount = expenseAmount.getText().toString();
+ String amountString = expenseAmount.getText().toString();
+ int amount = 0;
+ if (!amountString.equals("")) {
+ amount = Integer.parseInt(expenseAmount.getText().toString());
+ }
Log.v("Bootcamp", "The expense: " + name + " has the value: " + amount);
Intent details = new Intent(MainActivity.this, DetailActivity.class);
+ details.putExtra("name", name);
+ details.putExtra("amount", amount);
startActivity(details);
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.