0

I understand how initialize a spinner from String Array in the resources file String.xml. However my issue always is how to link things properly when you select something from drop down menu. For example let say that your string array looks something like that

Electricity Gas Other

So in onItemSelected listener, if I want to know what was clicked to take a certain action, then should I check the pos and act on it (given i know position in my string array as below

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        switch(pos){
            case 1: // Electricity

            break;
            case 2: //Gas

            break;
                    .
                    .
                    .
        }       
    }

OR should I do Sting comparison like :

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        if(parent.getSelectedItemAtPos(pos).toString().equal("Electricity"){

            } else if (......){
            }
            .
        .
    }

The advantage of first is it is cleaner but the problem I have to make sure that when I change the resource file, I would always keep track of updating indexes. The second is safer but it seems to be "wordy" doing String compare.

I am not, maybe I am missing an optimum solution or what I suggested above is simply ok?

Please help experts, I would rather do something right the first time as I would have many spinners in my app :(

Thanks

Snake
  • 14,228
  • 27
  • 117
  • 250

4 Answers4

1

I think both method are fine .It depends on you need which one you should choose

As you said First one provide more readability even though there is increase in number of cases

But as in second approach if there is only 1-2 conditions to check it's look fine as condition increase they code may look messy

if you care about readability go with first approach or else you can choose on your need

Note : If we are comparing the performance of both approach ,I think there is not much difference between them (Refer following links { link1, link2 })

Community
  • 1
  • 1
edwin
  • 7,985
  • 10
  • 51
  • 82
  • Thanks for the links although they are comparing if/else with switch while my concern was more of integer/string comparison on top of which approach. +1 though for the comment – Snake Jan 29 '13 at 06:14
  • i think integer comparison will be better compare to string comparison .but use it on the base of your need . – edwin Jan 29 '13 at 06:29
1

Second is fine but a little enhancement. Declare a local string variable and store the selected item of Spinner.

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

        String selectedItem = parent.getSelectedItemAtPos(pos).toString();

        if(selectedItem.equals("Electricity")) {

        } else if (selectedItem.equals("Dummy")){
            // ....    
        }

}
1

Between the two of these, without question the first option. For one, it's a switch statement so it's an instant jump to the right handle, whereas with the second you have a linear line of comparisons until you reach the one that you want. Secondly, you're using String comparison in the second which is slower and less safe than just doing an integer comparison. It's a little more work if you plan on changing it a lot, but I think it's the better way of the two, personally.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
1

If you are setting entries in the spinner through a resource file first approach will be better cause while declaring strings in resource file you will be knowing which string is in which index.

android:entries="@array/..."

but if you are using adapter to set the entries(which you are receiving from server or any where else) then better you go with second approach. As Indexing of strings will be uncertain in that case.

mSpinner.setAdapter(...)

Lavakush
  • 1,910
  • 2
  • 11
  • 14