在菜单的开关盒中使用字符串?
我目前正在为论坛制作一个应用程序,并且我正在添加一个收藏夹部分,您可以在其中添加您最喜欢的部分以便快速访问。我设置了一个菜单,其中包含不同部分的列表,因此我所做的是创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
这是一个很好且干净的解决方法:
您可以切换枚举并在适配器中以这种方式使用它:
更新
唯一的问题是这些字符串是硬编码的。为了解决这个问题,我建议实施 静态应用程序上下文帮助器并像这样实现
enum
(这是一个想法,我没有尝试过,但它应该可以工作):Here's a nice and clean workaround:
You can switch an enum and use it that way in your adapter:
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):更好的解决方案:
一个枚举类:
在另一个类中:
@override 枚举选项卡的 toString() 类可以做得很好。
Better solution:
One enum Class:
In the another class:
@override the toString() class of the enum Tabs to do it nice .