how to save selected radio button and spinner data by value using rest API?

Viewed 32

I am using rest API and facing some issue when embedding an API. How do I save spinner and radio button data by value and using retrofit2.This API is for calculating BMR and BMR Please help!

Here is BMIquestions.java


public class BMIquestions extends AppCompatActivity implements View.OnClickListener{

    Button nextbtn;
    ImageButton date;
    DatePickerDialog datePickerDialog;
    EditText age, height, weight;
    // Let's assume 1 = male and 0 = female
    // Declare a RadioGroup object reference
    RadioGroup rgGender;
    // Declare RadioButton object references for Male and Female
    RadioButton rbMale, rbFemale;
    private String gender=null;



    @Override
    protected void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bmiquestions);
        getSupportActionBar().hide();
        nextbtn = findViewById(R.id.Next);
        date = findViewById(R.id.calender);
        age = findViewById(R.id.age);
        height = findViewById(R.id.currentHeight);
        weight = findViewById(R.id.currentHeight);
        rgGender = findViewById(R.id.gender_group);
        rbMale = findViewById(R.id.male);
        rbFemale = findViewById(R.id.female);

        rgGender.clearCheck();
        rgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                rbMale = group.findViewById(R.id.male);
                rbFemale = group.findViewById(R.id.female);
                if(rbMale.isSelected()){
                    rbMale.setTag("1");
                    rbMale.getTag().toString();
                if(rbFemale.isSelected()){
                    rbFemale.setTag("0");
                    rbFemale.getTag().toString();
                }

                }

            }
        });

        findViewById(R.id.female).setOnClickListener(this);
        findViewById(R.id.male).setOnClickListener(this);
        findViewById(R.id.Next).setOnClickListener(this);
        String[] level = new String[]{"Not Very Activ", "Lightly Active", "Active", "Very Active"};
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                R.layout.drop_down_items,
                level
        );

        AutoCompleteTextView autoCompleteTextView = findViewById(R.id.type);
        autoCompleteTextView.setAdapter(adapter);

        autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(BMIquestions.this, autoCompleteTextView.getText().toString(), Toast.LENGTH_SHORT).show();
                if(autoCompleteTextView.getText().toString().equals("Lightly Active")){
                    autoCompleteTextView.setTag("1.55");
                }
            }
        });

        date.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onClick(View view) {
                // Calender class 's instance and get current,date ,month and year from calender
                final Calendar c = Calendar.getInstance();
                int mYear = c.get(Calendar.YEAR);//current year
                int mMonth = c.get(Calendar.MONTH);//current month
                int mDay = c.get(Calendar.DAY_OF_MONTH);//current date
                final int noofyears = (int) (mYear - c.get(Calendar.YEAR)); //calculate age

                //date picker dialog
                datePickerDialog = new DatePickerDialog(BMIquestions.this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                                  int monthOfYear, int dayOfMonth) {
                                // set day of month , month and year value in the edit text
                                age.setText(dayOfMonth + "/"
                                        + (monthOfYear + 1) + "/" + year);

                            }
                        }, mYear, mMonth, mDay);

                DatePicker dp = datePickerDialog.getDatePicker();
                WindowManager.LayoutParams params = datePickerDialog.getWindow().getAttributes();

                //params.gravity = Gravity.CENTER_HORIZONTAL;
              
                params.width = 50; // dialogWidth;
                params.height = 100; // dialogHeight;
                datePickerDialog.show();
            }
        });

    }


        public void onClick(View view) {

            switch (view.getId()) {
                case R.id.Next:
                    openHomePage();
                    break;
                case R.id.male:
                    gender="0";
                    break;
                case R.id.female:
                    rbFemale.setTag("0");
                    gender=rbFemale.getTag().toString();
                    break;
            }
        }

    public void openHomePage() {

        String userHeight = height.getText().toString();
        String userWeight = weight.getText().toString();
        String userAge = age.getText().toString();
        

        HashMap<String, String> meMap = new HashMap<String, String>();
        meMap.put("height", userHeight);
        meMap.put("age", userAge);
        meMap.put("gender", gender);
        //meMap.put("activity_level",act);
        meMap.put("current_weight",userWeight);

        Call<QuestionResponse> call = RetrofitClient
                .getInstance()
                .getApi()
                .calculation(meMap);

        call.enqueue(new Callback<QuestionResponse>() {
            @Override
            public void onResponse(Call<QuestionResponse> call, Response<QuestionResponse> response) {

                QuestionResponse questionResponse = response.body();

                if (response.isSuccessful()) {

                    if (questionResponse.getStatus().equals("SUCCESS")) {
                        Toast.makeText(BMIquestions.this, questionResponse.getMessage(), Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(BMIquestions.this, HomePage.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                        finish();
                    } else {
                        Toast.makeText(BMIquestions.this, questionResponse.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
                else {
                    Toast.makeText(BMIquestions.this, questionResponse.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<QuestionResponse> call, Throwable t) {
                Toast.makeText(BMIquestions.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }


        });

        Intent i=new Intent(BMIquestions.this,HomePage.class);
        startActivity(i);
    }
    }

Here is activity_bmiquestions.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.questions.BMIquestions">

    <TextView
        android:id="@+id/targetWeight"
        android:layout_width="386dp"
        android:layout_height="wrap_content"
        android:fontFamily="@font/roboto_regular"
        android:gravity="center"
        android:paddingStart="12dp"
        android:paddingTop="60dp"
        android:text="Tell us About Youself"
        android:textColor="@color/black"
        android:textSize="22sp"
        android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="20dp"
            android:layout_marginTop="122dp"
            android:fontFamily="@font/roboto_regular"
            android:text="Gender:"
            android:textColor="@color/black"
            android:textSize="14dp" />


        <RadioGroup
            android:id="@+id/gender_group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="142dp"
            android:paddingStart="24dp"
            android:orientation="horizontal"
            >

            <RadioButton
                    android:id="@+id/female"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Female"
                    />

            <RadioButton
                android:id="@+id/male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Male" />

        </RadioGroup>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="18dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="230dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/age"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:hint="DD/MM/YYYY" />

            <ImageButton
                android:id="@+id/calender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@drawable/ic_baseline_calendar_today_24" />

        </LinearLayout>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="120dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="20dp"
            android:layout_marginTop="202dp"
            android:fontFamily="@font/roboto_regular"
            android:text="Height in centimeter"
            android:textColor="@color/black"
            android:textSize="14dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="230dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/currentHeight"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:hint="Enter your height"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/black"
                android:text="cm"/>

        </LinearLayout>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="17dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="20dp"
            android:layout_marginTop="202dp"
            android:fontFamily="@font/roboto_regular"
            android:text="When were you born ?"
            android:textColor="@color/black"
            android:textSize="14dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="230dp"
            android:orientation="horizontal" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="120dp">

          <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="230dp"
            android:orientation="horizontal"/>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="230dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginStart="20dp"
            android:layout_marginTop="202dp"
            android:fontFamily="@font/roboto_regular"
            android:text="Weight in kilogram"
            android:textColor="@color/black"
            android:textSize="14dp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="230dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/currentWeight"
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:hint="Enter your weight" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="kg"
                android:textColor="@color/black" />

        </LinearLayout>

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="440dp"
        android:layout_marginStart="10dp">

    
  
  
  
  <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
        android:layout_width="330dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="80dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="80dp"
        android:hint="Activity Level" >

        <AutoCompleteTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/type"
            android:inputType="none">


        </AutoCompleteTextView>
    </com.google.android.material.textfield.TextInputLayout>
    </RelativeLayout>
    <android.widget.Button
        android:id="@+id/Next"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="21dp"
        android:background="@color/dark_grey"
        android:text="Next"
        android:textColor="@color/white"
        android:textSize="15sp"
        android:textStyle="bold"></android.widget.Button>



</RelativeLayout>

Here is Api Interface


 public interface Api{
       //GetCalculations
    @POST("calculation")
    Call<QuestionResponse> calculation(
            @Body HashMap<String, String> body
    );
}

Here is RetrofitClient.java


package com.example.signup.ui.api;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {
    //singeleton class defining retrofit client
    //define base url
    //initialize retrofit object

    private static final String BASE_URL = "http://xxx.xxx.xxx.xxx:xxxx/api/";
    private static RetrofitClient retrofitClient;
    private static Retrofit retrofit;

    // HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    //  interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    // OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

    private RetrofitClient() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //return instances when above method call
    public static synchronized RetrofitClient getInstance() {
        if (retrofitClient==null) {
            retrofitClient = new RetrofitClient();
        }
        return retrofitClient;
    }

    public Api getApi() {
        return retrofit.create(Api.class);
    }
}

How can I fix this problem?

0 Answers
Related