简体   繁体   English

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

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

I have a Spinner that has 16 values. 我有一个具有16个值的微调框。 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). 24,24 1/16,24 2/16 .... 25.我要选择一个,然后将其放入double(decimal)形式的变量“ w”中。 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. 为了简单起见,我只编写了三个if语句,但是只要知道它会以某种方式影响它,我就会知道其中有16个。 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. 由于您仅在onItemClickif语句中创建变量,因此只能在这些if语句中访问它们。 My suggestion would probably be to create it right before the if statements and then assign it a value in the statements. 我的建议可能是在if语句之前创建它,然后在语句中为其分配一个值。 And in order to use it you'll probably have to move all the math and stuff to inside onItemClick . 为了使用它,您可能必须将所有数学和内容移到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语句'范围的局部变量,并且在其外部未定义。

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

相关问题 无法从特定的下拉菜单中选择项目(文本)? - Unable to select item (Text) from a specific drop down menu? 如何从枚举创建下拉菜单? - How to create a drop down menu from an enum? 我想制作一个点击监听器,当我从下拉菜单中选择一个项目时,它会转到 android studio 中的所需页面 - I want to make an on click listner in such a way that when i select an item from drop down menu it goes to the desired page in android studio 如何制作隐藏式下拉菜单 - How can I make a drop down hidden menu 如何使用Selenium从下拉菜单中选择项目? - How to select items from drop down menu using Selenium? 下拉菜单未填充,我的错误在哪里? - Drop down menu not populating, where is my error? 如何使我的插件有助于将菜单项从Eclipse平台中的现有菜单添加到现有子菜单中? - How to make my plugin contributes adding a menu-item into an existing sub-menu from an existing menu in Eclipse platform? 从下拉菜单中获取并比较第一项与Selenium Webdriver中的字符串 - Get and compare first item from drop down menu with an string in selenium webdriver Selenium Java 从下拉菜单中选择 - Selenium Java to select from drop down menu 如何从给定列中的JTable下拉列表制作单个单元格 - How to make single cell from a given column as drop down in JTable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM