在下面的代码中,当选择菜单项删除时,代码会执行,但如果是代码的一部分,也会跳转到else.
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int menuItemIndex = item.getItemId();
final AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
//final int pos = menuInfo.position;
final String menuItemName =menuItems[menuItemIndex];
if(menuItemName.equals("Delete"))
{
try{
int pos = menuInfo.position;
Locations l= (Locations)locations.get(pos);
int id= l.get_id();
if( db.deleteLocation(id))
{
locations.remove(pos);
s.remove(pos);
arrayAdapter.notifyDataSetChanged();
}else
{
Toast.makeText(this,"Can not delet a location with apartment blocks",Toast.LENGTH_LONG).show();
}
}catch(Exception e)
{
e.printStackTrace();
}
}else if((menuItemName.equals("View Receipts")));
{
// get an extral to pass to the intent
int pos1 = menuInfo.position;
Locations l= (Locations)locations.get(pos1);
int id= l.get_id();
Intent intent = new Intent(this,LocationSales.class);
intent.putExtra("ID", id);
startActivity(intent);
}
return super.onContextItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)
如果此if( db.deleteLocation(id))表达式为false,则执行Toast部分并跳转到else if块中的此部分
int pos1 = menuInfo.position;
Locations l= (Locations)locations.get(pos1);
int id= l.get_id();
Intent intent = new Intent(this,LocationSales.class);
intent.putExtra("ID", id);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
我的ifs中有任何缺陷造成的吗?
罗纳德
删除半结肠:
更改
}else if((menuItemName.equals("View Receipts")));
Run Code Online (Sandbox Code Playgroud)
至
}else if((menuItemName.equals("View Receipts")))
Run Code Online (Sandbox Code Playgroud)
该半冒号结束else if语句,导致后续块始终执行.
| 归档时间: |
|
| 查看次数: |
55 次 |
| 最近记录: |