简体   繁体   中英

How to make my variable = the item from the Drop Down Menu

I have a Spinner that has 16 values. 24, 24 1/16, 24 2/16 .... 25. I want to take the one the selected and put it into a variable "w" in the form of a double(decimal). I want to use that variable in the calculations below. I've only made three if Statements for simplicity, but just know there will be 16 of them if it affects it somehow. But in the Calculations they show up as not defined.

public void calculate(View view) {
    EditText length_find = findViewById(R.id.feet);
    EditText pounds_find = findViewById(R.id.pounds);
    DecimalFormat df = new DecimalFormat("###.###");


    Spinner width_select = findViewById(R.id.width_select);

    ArrayAdapter<String> myAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.width));
    myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    width_select.setAdapter(myAdapter);

    width_select.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Object item = parent.getItemAtPosition(position);
            if (position == 0){
                double w = 24;

            }
            if (position == 1){
                double w = 24.0625;
            }
            if (position == 2){
                double w = 24.125;
            }
        }
    });



    try {

        double d = .29;
        double h = .002;



        //This Calculates if Pounds are given
        if (length_find.getText().toString().equals("")){
            Toast.makeText(MainActivity.this, "Given Pounds", LENGTH_LONG).show();
            double pounds = Double.parseDouble(pounds_find.getText().toString());


            //THIS IS THE MATH PORTION

            double length_d = (pounds / (w * h * d * 12));


            String length = Double.toString(Double.parseDouble(df.format(length_d)));
            final TextView zTextView = (TextView) findViewById(R.id.length_show);
            zTextView.setText("Feet: " + length);
            final TextView mTextView = (TextView) findViewById(R.id.pound_show);
            mTextView.setText("Pounds: ");
            length_find.getText().clear();
            pounds_find.getText().clear();
        }


        //This Calculates if Length is given
        if (pounds_find.getText().toString().equals("")){
          Toast.makeText(MainActivity.this, "Given Length", LENGTH_LONG).show();
          Double length = Double.parseDouble(length_find.getText().toString());





          //THIS IS THE MATH PORTION


          double answer_pounds_d = (w * h * length * 12 * d);







            String answer_pounds = Double.toString(Double.parseDouble(df.format(answer_pounds_d)));
          final TextView mTextView = (TextView) findViewById(R.id.pound_show);
          mTextView.setText("Pounds: " + answer_pounds);
          final TextView zTextView = (TextView) findViewById(R.id.length_show);
          zTextView.setText("Feet: ");
          length_find.getText().clear();
          pounds_find.getText().clear();



        }
        if((pounds_find).getText().toString().trim().length() > 0 && (length_find).getText().toString().trim().length() > 0){
            Toast.makeText(MainActivity.this, "Whata hell you need me for mate!", LENGTH_LONG).show();
        }

    }
    catch(Exception ex) {
            //Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
        }

    }

Since you're only creating the variable in the if statements inside onItemClick , they're only accessible inside those if statements. My suggestion would probably be to create it right before the if statements and then assign it a value in the statements. And in order to use it you'll probably have to move all the math and stuff to inside onItemClick .

Edit: untested, but my guess would be something like this:

public void calculate(View view) {
    EditText length_find = findViewById(R.id.feet);
    EditText pounds_find = findViewById(R.id.pounds);
    DecimalFormat df = new DecimalFormat("###.###");


    Spinner width_select = findViewById(R.id.width_select);

    ArrayAdapter<String> myAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.width));
    myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    width_select.setAdapter(myAdapter);

    width_select.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Object item = parent.getItemAtPosition(position);
           double w = 0;


            if (position == 0){
                double w = 24;

            }
            if (position == 1){
                double w = 24.0625;
            }
            if (position == 2){
                double w = 24.125;
            }
    try {

        double d = .29;
        double h = .002;



        //This Calculates if Pounds are given
        if (length_find.getText().toString().equals("")){
            Toast.makeText(MainActivity.this, "Given Pounds", LENGTH_LONG).show();
            double pounds = Double.parseDouble(pounds_find.getText().toString());


            //THIS IS THE MATH PORTION

            double length_d = (pounds / (w * h * d * 12));


            String length = Double.toString(Double.parseDouble(df.format(length_d)));
            final TextView zTextView = (TextView) findViewById(R.id.length_show);
            zTextView.setText("Feet: " + length);
            final TextView mTextView = (TextView) findViewById(R.id.pound_show);
            mTextView.setText("Pounds: ");
            length_find.getText().clear();
            pounds_find.getText().clear();
        }


        //This Calculates if Length is given
        if (pounds_find.getText().toString().equals("")){
          Toast.makeText(MainActivity.this, "Given Length", LENGTH_LONG).show();
          Double length = Double.parseDouble(length_find.getText().toString());





          //THIS IS THE MATH PORTION


          double answer_pounds_d = (w * h * length * 12 * d);







            String answer_pounds = Double.toString(Double.parseDouble(df.format(answer_pounds_d)));
          final TextView mTextView = (TextView) findViewById(R.id.pound_show);
          mTextView.setText("Pounds: " + answer_pounds);
          final TextView zTextView = (TextView) findViewById(R.id.length_show);
          zTextView.setText("Feet: ");
          length_find.getText().clear();
          pounds_find.getText().clear();



        }
        if((pounds_find).getText().toString().trim().length() > 0 && (length_find).getText().toString().trim().length() > 0){
            Toast.makeText(MainActivity.this, "Whata hell you need me for mate!", LENGTH_LONG).show();
        }

    }
    catch(Exception ex) {
            //Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show();
        }

    }
        }
    });

只需将变量“ w”设置为活动的全局变量即可。因为变量“ w”属于'if语句'范围的局部变量,并且在其外部未定义。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM