0

I am creating a diet app for myself and I do not know how to transfer result from one calculation which was done in a different activity and use it in another one.

To make things more complicated, I want my spinner in MoreLooseWeightDetails.class to have a list of different diet types which have different fat, proteins and carb ratio. Every time the user choose different type it should automatically change the ratio.

This is where the calculation is being done and has to be trnasfered to MoreLooseWeightDetails.class

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class LooseWeight extends AppCompatActivity {

TextView TotalCal;
EditText numb2;
Spinner ActLvl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_loose_weight);

    numb2 = (EditText) findViewById(R.id.value1);
    ActLvl = (Spinner) findViewById(R.id.value2);
    TotalCal = (TextView)findViewById(R.id.resultDisplay);

    final Spinner ActLvl = (Spinner)findViewById(R.id.value2);
    ArrayAdapter<String> myAdapter = new ArrayAdapter<>(LooseWeight.this, android.R.layout.simple_expandable_list_item_1,getResources().getStringArray(R.array.lvl));
    myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    ActLvl.setAdapter(myAdapter);


    Button calcBtn = (Button)findViewById(R.id.result);
    calcBtn.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick (View view) {
        float numb3 = Float.parseFloat(numb2.getText().toString());
            float a = numb3 * 24;
            float b = a * Float.parseFloat(ActLvl.getSelectedItem().toString());
            float c = b - 500;
            TotalCal.setText(Float.toString(c));
        }
    });

    Button extra = (Button)findViewById(R.id.advance);
    extra.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(getApplicationContext(),MoreLooseWeightDetails.class);
            startActivity(intent);
        }
    });

}
}

Here is the MoreLooseWeightDetails.class

package com.fitup.fit_up;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MoreLooseWeightDetails extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_more_loose_weight_details);
}
}

Here is the MoreLooseWeightDetails.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_more_loose_weight_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.fitup.fit_up.MoreLooseWeightDetails">

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/Title3"
    tools:text="More"
    android:textSize="36sp" />

<TextView
    android:text="Select Type of Diet"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Title3"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="19dp"
    android:id="@+id/Info3"
    android:textSize="30sp" />

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/Info3"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="13dp"
    android:id="@+id/spinner" />

<TextView
    android:text="You Should Eat :"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:id="@+id/Info4"
    android:textSize="30sp"
    android:layout_below="@+id/spinner"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:text="Protein (grams):"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/unit1"
    android:textSize="24sp"
    android:layout_below="@+id/Info4"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="78dp" />

<TextView
    android:text="Fat (grams):"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/unit3"
    android:textSize="24sp"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<TextView
    android:text=""
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/unit3"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/result1"
    android:textSize="30sp"
    android:textAlignment="center"
    android:layout_alignLeft="@+id/result2"
    android:layout_alignStart="@+id/result2" />

<TextView
    android:text=""
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/unit2"
    android:id="@+id/result2"
    android:textSize="30sp"
    android:textAlignment="center"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignLeft="@+id/result3"
    android:layout_alignStart="@+id/result3" />

<TextView
    android:text=""
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/unit2"
    android:id="@+id/result3"
    android:textSize="30sp"
    android:textAlignment="center"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_toRightOf="@+id/unit2"
    android:layout_toEndOf="@+id/unit2" />

<TextView
    android:text="Carbohydrates (grams):"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="43dp"
    android:id="@+id/unit2"
    android:textSize="24sp"
    android:layout_below="@+id/unit1"
    android:layout_alignRight="@+id/unit1"
    android:layout_alignEnd="@+id/unit1" />
</RelativeLayout>
  • In order to pass data between Activities you should use Intents. [Look at this SO question and its answer](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android). To get a callback when the Spinner's data changes you want to implement OnItemSelectedListener(). There is a really great answer [here](http://stackoverflow.com/questions/1337424/android-spinner-get-the-selected-item-change-event) – Nicolás Carrasco-Stevenson Mar 03 '17 at 13:48
  • @NicolásCarrasco thank you for a quick respond, I will check right away. – Bartosz Nadrowski Mar 03 '17 at 14:00
  • @NicolásCarrasco, it gave me some ideas, however, each selection in the spinner will have to have a different calculation. Therefore where should I put the formulas??Am I right thinking that it will be in "if" statement?? Sorry I have not used any coding for last 2 years. All went out of my head. – Bartosz Nadrowski Mar 03 '17 at 15:10

0 Answers0