Way to cast View to Double – Android Studio

  android-studio, casting, double, view

I need to cast a View to double. Is there a particular way to go about this? Here is my code just for reference. This is supposed to be a conversion app. I have a problem with converting the View to String seeing as it somehow messed up my output. Frankly, I can’t seem to find a solution no matter how hard I try. My only other solution is to try and find a different way to have an input be put to double. Any help is appreciated!

package com.stproductions.commonconverter;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Array;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = findViewById(R.id.button);
        final TextView[] txtAnswer = {findViewById(R.id.textAnswer)};
        txtAnswer[0].setVisibility(View.INVISIBLE);

        View enterAnswer = (R.id.enterAmount); //Here is where I need to convert it over

        double lastValue = (double) enterAnswer; // I am trying to do that here!


        //Convert From Box
        Spinner spinner1 = findViewById(R.id.spinner1);
        ArrayAdapter<CharSequence>adapter1 = ArrayAdapter.createFromResource(this, R.array.fromchoices, android.R.layout.simple_spinner_item);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner1.setAdapter(adapter1);


        //Convert To Box
        Spinner spinner2 = findViewById(R.id.spinner2);
        ArrayAdapter<CharSequence>adapter = ArrayAdapter.createFromResource(this, R.array.choices, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
        spinner2.setAdapter(adapter);

                    //   oz    lb      g       kg         ton        mg
        double[] ounce = {1, 0.0625, 28.35, 0.0283495, 0.00003125, 28349.5};
        double[] pound = {16, 1, 453.59, 0.453592, 0.0005, 453592};
        double[] gram = {0.35274, 0.0022, 1, 0.001, 0.000001, 1000};
        double[] kilogram = {35.274, 2.2, 1000, 1, 0.00110231, 1000000};
        double[] ton = {32000, 2000, 907185, 907.185, 1, 907184740};
        double[] milligram = {0.000035, 0.000002, 0.001, 0.0000001, 0.000000001, 1};


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (spinner1.getSelectedItem() != null && spinner2.getSelectedItem() != null) {
                    String first_option = spinner1.getSelectedItem().toString();
                    String second_option = spinner2.getSelectedItem().toString();

                    if (first_option.contentEquals("oz") && second_option.contentEquals("lb")) {

                        double finishConversion = enterAnswer / pound[0];
                        String str = Double.toString(finishConversion);

                        txtAnswer[0].setVisibility(View.VISIBLE);

                        txtAnswer[0].setText(str);

                    } else {
                    }
                }
            }
        });
    }
}

Source: Android Studio Questions

LEAVE A COMMENT