Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Android
exit_app = menu.getItem(7);
 
exit_app.setOnMenuItemClickListener(new OnMenuItemClickListener()
{
    public boolean onMenuItemClick(MenuItem clickedItem)
    {
        //Exit the App
        try{
            _run = false;
        finish();
        }catch(Exception c)
        {
            c.toString();
        }
        return true;
    }
});
 
text_enter = menu.getItem(1);
 
text_enter.setOnMenuItemClickListener(new OnMenuItemClickListener()
{
    public boolean onMenuItemClick(MenuItem clickedItem)
    {
        _run = false;
 
        try{
            Thread.sleep(1000);
        setContentView(R.layout.text_enter);
        buttonCancel.setOnClickListener(buttonSendOnClickListener);
        }catch(Exception x)
        {
            x.toString();
        }
        return true;
    }
});
//and so on.....
Help. Thanks.
Posted 27-Nov-12 4:50am
Edited 27-Nov-12 5:21am
v2
Comments
lewax00 at 27-Nov-12 10:55am
   
Using a switch isn't going to make this shorter, in fact it would actually make it a little longer...why do you want to use a switch for it?
halabella at 27-Nov-12 11:25am
   
I just thought it will be more accurate like as one solution for buttons in this topic http://www.codeproject.com/Questions/499423/Whyplusbuttonplusdoesn-27tplusworkpluswhenplusIplu

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think I get what you're trying to do now. In that case, I think you'd want something like this:
 
class MyClass implements OnMenuItemClickListener {
 
    /* whatever function you're setting up the controls in */ {
        /* ... */
        
        exit_app = menu.getItem(7);
        text_enter = menu.getItem(1);
        /* ... */
        
        exit_app.setOnMenuItemClickListener(this);
        text_enter.setOnMenuItemClickListener(this);
        /* ... */
    }
 
    public boolean onMenuItemClick(MenuItem clickedItem)
    {
        switch (clickedItem.getId()) {
            case R.id.exit_app:
                //Exit the App
                try{
                    _run = false;
                finish();
                }catch(Exception c)
                {
                    c.toString();
                }
                return true;
            case R.id.text_enter:
                _run = false;
 
                try{
                    Thread.sleep(1000);
                    setContentView(R.layout.text_enter);
                    buttonCancel.setOnClickListener(buttonSendOnClickListener);
                }catch(Exception x)
                {
                    x.toString();
                }
                return true;
            /* ... */
        }
    }
}
 
But a few more things: the numbers used in menu.GetItem should be made into constants, for example:
static final int EXIT_APP_MENU_INDEX = 7;
 
Also, the function with the switch won't compile as-is, it will need to return a value either after the switch or in a default case.
  Permalink  
Comments
TorstenH. at 27-Nov-12 13:50pm
   
+5!
 
To add in: if you do not have a "return" at the end of each case-statement, than you'd need a "break" to exit that switch.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 626
1 OriginalGriff 311
2 Maciej Los 210
3 Mehdi Gholam 145
4 PIEBALDconsult 140
0 Sergey Alexandrovich Kryukov 11,384
1 OriginalGriff 5,783
2 Maciej Los 5,684
3 CPallini 3,879
4 BillWoodruff 3,584


Advertise | Privacy | Mobile
Web03 | 2.8.141030.1 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100