3

I'm making an app for a forum currently, and I'm adding a favorites section where you can add your favorite sections for quick access. I have a menu set up that has a list of the different sections, so what I did was make a switch case to decide what to do when a certain menu item is pressed, in this case the section. Through doing this I learned that you can't use strings with a switch case, so my question is how could I decifer which button was pressed and do an action according to which one was pressed?

This is my code for the menu:

public class Menu extends ListActivity{

String[] classes = {"Home", "Gaming", "Microsoft Consoles", 
        "Sony Consoles", "Other Platforms", "Tech Center", "General"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    switch (classes) {
    case "Home":

    break; 
    case "Gaming".hashCode(): 

    break; 
    }   
}
}

I get an error because classes is a String[], and I can't use that with a switch case, so is there a way to do that, or an alternative?

Darren
  • 1,774
  • 4
  • 21
  • 32

3 Answers3

8

Strings in switch statements were added in Java 7. For an example, take a look here. Since Android development isn't currently based on Java 7 syntax, you'll have to go the alternate route. And that means: if-else statements. They aren't the prettiest, but they'll get the job done.

darioo
  • 46,442
  • 10
  • 75
  • 103
  • 2
    @Darren Personally, I'd find the enum or a command pattern implementation much cleaner, and easier to maintain. – Dave Newton Oct 01 '11 at 14:24
6

Here's a nice and clean workaround:

public enum Classes {
    HOME("Home"),
    GAMING("Gaming"),
    ...
    MICROSOFT_CONSOLES("Microsoft Consoles");

    private final String name;

    Classes(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }
}

You can switch an enum and use it that way in your adapter:

new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, Classes.values())

Update

The only problem is that those strings are hard-coded. To workaround that fact I would recommend to implement a static Application context helper and implement the enum like this (it's an idea, I didn't try that, but it should work):

public enum Classes {
    HOME(R.string.home),
    GAMING(R.string.gaming),
    ...
    MICROSOFT_CONSOLES(R.string.microsoft_consoles);

    private final int name;

    Classes(int name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return App.getContext().getString(name);
    }
}
Community
  • 1
  • 1
Knickedi
  • 8,742
  • 3
  • 43
  • 45
1

Better solution:

One enum Class:

public enum Tabs 
{
 Home, 
 Gaming, 
 Microsoft_Consoles, 
 Sony_Consoles, 
 Other_Platforms, 
 Tech_Center, 
 General
}

In the another class:

 switch (classes)
 {
   case Home:

   break;
 ...
 }

@override the toString() class of the enum Tabs to do it nice .

A.Quiroga
  • 5,704
  • 6
  • 37
  • 58