Android 从上下文菜单切换到正确的活动
我的问题是如何从上下文菜单切换到正确的活动 我有这样的活动:Android 从上下文菜单切换到正确的活动,android,android-intent,Android,Android Intent,我的问题是如何从上下文菜单切换到正确的活动 我有这样的活动: Main AccelerometrictionsActivity 陀螺仪选项活动 方向选项活动 在主要活动中,我有一个传感器列表。单击传感器时,会出现上下文菜单,我可以在其中单击选项等 我的问题是如何从上下文菜单切换到所选传感器的选项活动。 我的代码: @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.Adap
Main
AccelerometrictionsActivity
陀螺仪选项活动
方向选项活动
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int menuItemIndex = item.getItemId();
String[] menuItems = getResources().getStringArray(R.array.sensor_array);
String menuItemName = menuItems[menuItemIndex];
if(item.getTitle()=="Start Service"){
Toast.makeText(this,"Start " + menuItemName+ " selected", Toast.LENGTH_SHORT).show();
} else if(item.getTitle()=="Stop Service") {
Toast.makeText(this,"Stop " + menuItemName+ " selected", Toast.LENGTH_SHORT).show();
} else if(item.getTitle()=="Options") {
Intent options = new Intent(this, AccelerometerOptionsActivity.class);
startActivity(options);
}
return true;
}
更新:
代码如下:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
if (v.getId()==R.id.list) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle(sensorsArray[info.position]);
menu.add(Menu.NONE, CONTEXTMENU_START, 0, "Start Service");
menu.add(Menu.NONE, CONTEXTMENU_STOP, 1, "Stop Service");
menu.add(Menu.NONE, CONTEXTMENU_OPTIONS, 2, "Options");
menu.add(Menu.NONE, CONTEXTMENU_GRAPHS, 3, "Graph view");
menu.add(Menu.NONE, CONTEXTMENU_DATA, 4, "Data view");
}
}
使用.contains(yourString)方法而不是==。
e、 g.更换
if(item.getTitle()=="Options") {...
与
在您的
onContextItemSelected()
中尝试此操作:
我把这个换成了开关。但这不是我的问题。我的问题是如何进行适当的活动。除了==部分之外,您的代码看起来很好。更换它,然后查看。如果可行的话,试着调试,看看它是否达到了断点。如果你遇到任何异常或其他情况,请将日志与你的问题放在一起。我认为你不理解我的问题。我希望被引导到正确的选项活动,例如,我选择加速计,并从关联菜单中转到加速计选项。我选择方向服务,并从上下文菜单中转到方向选项等。==此部分工作正常。关于您是否有一个带有传感器的
列表视图
,您在其上设置了上下文菜单
?将代码张贴在显示上下文菜单的位置。是的,当我单击传感器名称时,会出现上下文菜单,我有一个带有传感器的列表视图。我在上面的问题中添加了代码。我尝试了你的解决方案,但不起作用。我调试了代码,但它忽略了switch语句。@erni在我上面的代码中替换int menuItemIndex=item.getItemId()代码>带有int menuItemIndex=info.position代码>
if(item.getTitle().contains("Options") {...
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
.getMenuInfo();
int menuItemIndex = item.getItemId(); // the position of the item in the list with the context menu
String[] menuItems = getResources().getStringArray(R.array.sensor_array);
String menuItemName = menuItems[menuItemIndex];
switch (item.getItemId()) {
case CONTEXTMENU_START:
Toast.makeText(this, "Start " + menuItemName + " selected",
Toast.LENGTH_SHORT).show();
break;
case CONTEXTMENU_STOP:
Toast.makeText(this, "Stop " + menuItemName + " selected",
Toast.LENGTH_SHORT).show();
break;
case CONTEXTMENU_OPTIONS:
// here start the correct options activity either by checking for
// String equality with menuItemName or by doing a switch on the menuItemIndex(this works
// if your list of sensors is based on the array R.array.sensor_array)
switch (menuItemIndex) {
case 0:
//the user clicked the first sensor in the list so start that option activity
break;
case 1:
//the user clicked the second sensor in the list so start that option activity
break;
}
}
return super.onContextItemSelected(item);
}