简体   繁体   中英

Flawed logic in if block

In the code below, when menu item delete is selected , the code executes but also jumps to the else if part of the code.

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

If this if( db.deleteLocation(id)) expression is false, it executes the Toast part and jumps to this part in the else if block

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

Any flaw in my ifs causing this?

Ronald

Remove the semi colon:

change

}else if((menuItemName.equals("View Receipts")));

to

}else if((menuItemName.equals("View Receipts")))

That semi colon ends the else if statement, causing the following block to always get executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM