Spinner with steps (values) of 0.5 in 0.5?

2

I need values that go as follows:

0.5   
1  
1.5  
2  
2.5  
3  
Etc

I did a function but I do not know if it can be done in another way that requires less code, here's my code:

private String GetNumber(int spinner) {
        String result = "0";
        if (spinner == 1) {
            result = "0.5";
        }
        if (spinner == 2) {
            result = "1";
        }
        if (spinner == 3) {
            result = "1.5";
        }
        if (spinner == 4) {
            result = "2";
        }
        if (spinner == 5) {
            result = "2.5";
        }
        if (spinner == 6) {
            result = "3";
        }
        if (spinner == 7) {
            result = "3.5";
        }
        if (spinner == 8) {
            result = "4";
        }
        if (spinner == 9) {
            result = "4.5";
        }
        if (spinner == 10) {
            result = "5";
        }
        if (spinner == 11) {
            result = "5.5";
        }
        if (spinner == 12) {
            result = "6";
        }
        if (spinner == 13) {
            result = "6.5";
        }
        if (spinner == 14) {
            result = "7";
        }
        if (spinner == 15) {
            result = "7.5";
        }
        if (spinner == 16) {
            result = "8";
        }
        if (spinner == 17) {
            result = "8.5";
        }
        if (spinner == 18) {
            result = "9";
        }
        if (spinner == 19) {
            result = "9.5";
        }
        if (spinner == 20) {
            result = "10";
        }

        return result;

    }

Would it be the right way or is there another easier way?

    
asked by Angel Montes de Oca 03.01.2018 в 05:01
source

3 answers

3

I could simplify the function that returns half of the parameter, but within this it would be validated if it is an integer pair num%2==0 , if so, the result would be an integer. otherwise the division between 2.0 is the most important part that will convert the result into float if necessary.

public String GetNumber(int spinner){
    if(spinner%2==0) return String.valueOf(spinner/2);
    else return String.valueOf(spinner/2.0);
}

In case you want to fill Spinner with elements of 0.5 in 0.5 starting with 0.5 it would be enough to add in a for simple of type double to start and increase with this value, the function will have as parameter the limit value of the elements.

public class MainActivity extends AppCompatActivity {
    List<String> spinnerArray;//Elementos del Spinner
    Spinner mispinner ; // Spinner en el Layout
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Instanciamos el Array
        spinnerArray =  new ArrayList<String>();

        fillSpinnerValues(10);//LLamamos a la función con valor máximo 10
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_spinner_item, spinnerArray);
        mispinner= (Spinner) findViewById(R.id.mispinner);
        mispinner.setAdapter(adapter);
    }

    public void fillSpinnerValues(int limite){
        for (double  i= 0.5   ; i<= limite ; i+=0.5){
            //Comprobamos si es int el valor .Si es así realizamos el cast a int
            // Para no mostrar 1.0  o 2.0  si desea mostrar así simplemente
            // deja la linea spinnerArray.add(String.valueOf(i));
            if(i==Math.ceil(i)) spinnerArray.add(String.valueOf((int)i));
            else spinnerArray.add(String.valueOf(i));
        }
    }
}
    
answered by 03.01.2018 / 06:10
source
1

The only thing that occurs to me is to use a switch:

private String GetNumber(int spinner) {
    String result = "0";
    switch(spinner){
    case 1:
        result = "0.5";
        break;
    case 2:
        result = "1"; 
        break;
   //Sigue hasta 20 
    
answered by 04.01.2018 в 15:31
0

The sequence returns half of the number that is entered.

    public int calcular(int spinner) {
      var res = spinner/2;
      System.out.println(res);
      return res;
    }

    calcular(1);
    calcular(2);
    calcular(3);
    calcular(4);
    calcular(5);
    calcular(6);
    calcular(7);
    calcular(8);
    calcular(9);
    calcular(10);
    
answered by 03.01.2018 в 05:19