0

Hi i have three items in the spinner, and based on the selected item i am making some calculation and showing the user . for 3 items i have 6 different calculations, i want to know FROM item and To item , so that i can take from value and to value and make the calculation. How to handle this scenario.

Below is simple switch case , but not solving my scenario

 spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent,
                            View view, int position, long id) {
                        switch (position) 
                        {          
                            case 0:

                                  edt1.setVisibility(View.VISIBLE);
                                   edt2.setVisibility(View.GONE);
                                   edt3.setVisibility(View.GONE);
                                    qtyString = edt1.getText().toString();
                                   if(qtyString == null || qtyString.trim().equals("") || qtyString.trim().equals(".")){

                                   }else{
                                       float k = Float.parseFloat(qtyString);
                                       edt1.setText(removeTrailingZeros(poundTokilo(k))); 
                                   }
                                break;               

                            case 1:
                                 edt1.setVisibility(View.VISIBLE);
                                   edt2.setVisibility(View.GONE);
                                   edt3.setVisibility(View.GONE);

                                   if(edt1.getText().toString().equals("") || edt1.getText().toString().equals(".")){

                                   }else{
                                   float qtyString1 = Float.parseFloat(edt1.getText().toString());
                                   edt1.setText(removeTrailingZeros(kiloTopound(qtyString1)));
                                   }
                                break;     

                            case 2:
                                edt1.setVisibility(View.GONE);
                                edt2.setVisibility(View.VISIBLE);
                                edt3.setVisibility(View.VISIBLE);
                                if(edt1.getText().toString().equals("")){

                                   }else{      
                                float qtyString2 = Float.parseFloat(edt1.getText().toString());
                                edt2.setText(removeTrailingZeros(kiloTostomepound(qtyString2).get(0).get(STONE)));
                                edt3.setText(removeTrailingZeros(kiloTostomepound(qtyString2).get(0).get(STONEPOUND)));
                                   }
                                break;     
                        } 

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                        // TODO Auto-generated method stub

                    }
                });}
teekib
  • 2,821
  • 10
  • 39
  • 75

1 Answers1

0

switch-case not support rang based conditions so you can achieve same by using only one break in and default in switch-case :

switch (position) {
  case 1: case 2:case 3: 
     // executed when position is from 1 to 3
     break;
  default:
     // executed when position is not from 1 to 3
}

OR

Create a method which return false when position is not between 1 to 3 and then use if-else ladder for execute code according to condition :

public boolean isNumInRange(int number, int from, int to) {
  return from <= number && number <= to;
}

Now call isNumInRange(number,2,3) to check number is present in given range

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • what if it starts from 2-->3 and 3-->2 – teekib Jan 12 '15 at 05:39
  • @teekib: create a conman method with take three params position,from and to then return true false according to number. see my edit answer – ρяσѕρєя K Jan 12 '15 at 05:45
  • Hi lets think i can go from 1--> 3 or 2-->3, so below will be the condition in case 3? if(isNumInRange(1, 1, 2)){ }else {} – teekib Jan 12 '15 at 06:35
  • Hi lets think i can go from 1--> 3 or 2-->3, so below will be the condition in case 3? if(isNumInRange(3, 1, 3)){ }else {}....the both (3, 1, 3) and (3, 2, 3) will be true right – teekib Jan 12 '15 at 06:55
  • i did as below if(isNumInRange(2, 0, 2)){ System.out.println("printing kilo to stone pound"); } else { System.out.println("printing pound to stone pound"); } but alaways printing the first loop only – teekib Jan 12 '15 at 07:18
  • since both (2, 0, 2) and (2, 1, 2)) are true so it is not going to else part? – teekib Jan 12 '15 at 07:24
  • @teekib: (2,0,2) is always return true because here `from <= number && number <= to;` we are also checking if number of equals to less then so change this condition – ρяσѕρєя K Jan 12 '15 at 07:26
  • @teekib: change `number <= to` to `number < to` – ρяσѕρєя K Jan 12 '15 at 07:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68633/discussion-between-teekib-and--k). – teekib Jan 12 '15 at 08:34
  • @teekib modify `isNumInRange` method for changing range condition – ρяσѕρєя K Jan 12 '15 at 16:40
  • i changed number <= to to number < to but still bot working, please can u edit your answer – teekib Jan 13 '15 at 12:44