繁体   English   中英

如何使我的变量=下拉菜单中的项目

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

我有一个具有16个值的微调框。 24,24 1/16,24 2/16 .... 25.我要选择一个,然后将其放入double(decimal)形式的变量“ w”中。 我想在下面的计算中使用该变量。 为了简单起见,我只编写了三个if语句,但是只要知道它会以某种方式影响它,我就会知道其中有16个。 但是在计算中,它们显示为未定义。

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();
        }

    }

由于您仅在onItemClickif语句中创建变量,因此只能在这些if语句中访问它们。 我的建议可能是在if语句之前创建它,然后在语句中为其分配一个值。 为了使用它,您可能必须将所有数学和内容移到onItemClick内部。

编辑:未经测试,但我的猜测将是这样的:

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语句'范围的局部变量,并且在其外部未定义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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