在菜单的开关盒中使用字符串?

发布于 2024-12-07 20:00:31 字数 1032 浏览 0 评论 0原文

我目前正在为论坛制作一个应用程序,并且我正在添加一个收藏夹部分,您可以在其中添加您最喜欢的部分以便快速访问。我设置了一个菜单,其中包含不同部分的列表,因此我所做的是创建一个 switch case 来决定按下某个菜单项(在本例中为该部分)时要执行的操作。通过这样做,我了解到您不能将字符串与 switch case 一起使用,所以我的问题是如何判断按下了哪个按钮并根据按下的按钮执行操作?

这是我的菜单代码:

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; 
    }   
}
}

我收到错误,因为类是 String[],并且我无法将其与 switch case 一起使用,那么有没有办法做到这一点,或者有其他选择吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杀手六號 2024-12-14 20:00:31

Java 7 中添加了 switch 语句中的字符串。例如,请查看 这里。由于 Android 开发当前并非基于 Java 7 语法,因此您必须采用替代路线。这意味着:if-else 语句。他们不是最漂亮的,但他们会完成工作。

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.

痴梦一场 2024-12-14 20:00:31

这是一个很好且干净的解决方法:

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;
    }
}

您可以切换枚举并在适配器中以这种方式使用它:

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

更新

唯一的问题是这些字符串是硬编码的。为了解决这个问题,我建议实施 静态应用程序上下文帮助器并像这样实现enum(这是一个想法,我没有尝试过,但它应该可以工作):

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);
    }
}

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);
    }
}
羁客 2024-12-14 20:00:31

更好的解决方案:

一个枚举类:

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

在另一个类中:

 switch (classes)
 {
   case Home:

   break;
 ...
 }

@override 枚举选项卡的 toString() 类可以做得很好。

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文