Search code examples
javaandroidclassmethodsstatements

How do I "group" if statements to use in a spinner in Android Studio?


I'm quite new to Java/Android Studio and I'm having troubles "grouping"? if statements to use in a spinner. Basically I need my app to use certain values based on which position the spinner is set to. This is my current code.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        //Instantiating Widgets

        firstInput = findViewById(R.id.firstInput);
        secondInput = findViewById(R.id.secondInput);
        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);
        spinner = findViewById(R.id.spinner);


        //Spinner - adapter etc.
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.grades, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        //Adding a click event for button (Executing the convert method when clicked)
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {




           int force = 0;
           double in1 = Double.valueOf(firstInput.getText().toString());
           double in2 = Double.valueOf(secondInput.getText().toString());




           //Grade 5 Fine Thread
                if (in1 / in2 == 0.25) {
                        force = 2325;
                } else if (in1 / in2 == 0.3125) {
                        force = 3675;
                } else if (in1 / in2 == 0.375) {
                        force = 5588;
                } else if (in1 / in2 == 0.4375) {
                        force = 7575;
                } else if (in1 / in2 == 0.5) {
                        force = 10200;
                } else if (in1 / in2 == 0.5625) {
                        force = 12975;
                } else if (in1 / in2 == 0.625) {
                        force = 16350;
                } else if (in1 / in2 == 0.75) {
                        force = 23775;
                } else if (in1 / in2 == 0.875) {
                        force = 32475;
                }




             //Grade 5 Coarse Thread
             if(in1 / in2 == 0.25) {
                 force = 2025;
             } else if(in1 / in2 == 0.3125) {
                 force = 3338;
             } else if(in1 / in2 == 0.375){
                 force = 4950;
             } else if(in1 / in2 == 0.4375){
                 force = 6788;
             } else if(in1 / in2 == 0.5){
                 force = 9075;
             } else if(in1 / in2 == 0.5625){
                 force = 11625;
             } else if(in1 / in2 == 0.625){
                 force = 14400;
             } else if(in1 / in2 == 0.75){
                 force = 21300;
             } else if(in1 / in2 == 0.875){
                 force = 29475;
             }






        textView.setText(Double.toString(((in1 / in2) * 0.2 * force) / 12));


            }
        });
    }


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String text = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();

        if (position == 0) {

        }
        if (position == 1) {

        }



    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

So if the position is set to 0, I need the force to be set to fine thread values and if position is set to 1, the force needs to be set to the coarse thread values. I'm not sure where to go with this. Any help is greatly appreciated.

Thank you.


Solution

  • Please let me know if I misunderstood the question, but I think this will work for you:

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double in1 = Double.valueOf(firstInput.getText().toString());
            double in2 = Double.valueOf(secondInput.getText().toString());
            if (spinner.getSelectedItemPosition() == 0) {
                //fine thread code
                ...
            } else {
                //coarse thread code
                ...
            }
            textView.setText(Double.toString(((in1 / in2) * 0.2 * force) / 12));
        }
    }