在开关的情况下为菜单使用字符串? [英] Using strings in a switch case for a menu?

查看:122
本文介绍了在开关的情况下为菜单使用字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序,一个论坛,目前,和我加入收藏夹部分,您可以添加自己喜欢的部分,以便快速访问。我有一个菜单设置具有不同的部分名单,所以我所做的就是做一个开关的情况下,决定做什么,当某些菜单项是pressed,在这种情况下,一节。通过这样做,我了解到,你不能根据其使用字符串带开关的情况下,所以我的问题是我怎么能decifer哪个按钮是pressed和做一个动作一个是pressed?

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?

这是我的$ C $下菜单:

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

我得到一个错误,因为类是一个String [],我不能使用带开关的情况下,所以是有办法做到这一点,还是别的选择吗?

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?

推荐答案

在switch语句中的字符串添加在Java 7中举一个例子,看看<一href="http://java.dzone.com/articles/new-java-7-feature-string?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3a%20javalobby/frontpage%20%28Javalobby%20/%20Java%20Zone%29">here.由于Android的发展还不目前基于Java 7的语法,你就必须去替代路线。这意味着:if-else语句。他们不是prettiest,但他们会完成这项工作。

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.

这篇关于在开关的情况下为菜单使用字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆