-1

I am trying to make a small project for a shopping system in a supermarket. How can I display my result in another activity? Note that my result is displayed in a Toast in the same activity.

Here is my code:

public void showResult(View v) {
    String result = "Selected Product are :";
    int totalAmount = 0;
    for (Product p : boxAdapter.getBox()) {
        if (p.box) {
            result += "\n" + p.name;
            totalAmount += p.price;

        }
    }
    Toast.makeText(this, result + "\n" + "Total Amount:=" + totalAmount,
            Toast.LENGTH_LONG).show();

The toast msg here shows the result.

I want to display the result in a textView for example in another activity.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163

1 Answers1

0

In that purpose you have to pass data from one activity to another activity through android Intent. You can pass data one activity to another activity like this code

Intent intent = new Intent(ActivityA.this, ActivityB.class);
        intent.putExtra("TOTAL_AMOUNT", totalAmount );
        startActivity(intent);

Now you can get the result in ActivityB like this way

Bundle bundle = getIntent().getExtras();
String totalAmount= bundle.getString("TOTAL_AMOUNT");

After getting the totalAmount, you can set it in your TextView. Hope it solve your problem.

androidcodehunter
  • 21,567
  • 19
  • 47
  • 70
  • Just to add a little something to the answer by @AndroidCodeHunter, Just like with any variable in programming, it is always wise to name your intents with something anyone reading your code can understand.In this case, you can say `Intent showResultIntent = new Intent(this, ActivityB.class);` – Ojonugwa Jude Ochalifu Dec 21 '13 at 15:59