Click here to Skip to main content
12,064,870 members (35,098 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java Mobile 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 5:50am
halabella1.3K
Edited 27-Nov-12 6:21am
v2
Comments
lewax00 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 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. 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160204.4 | Last Updated 27 Nov 2012
Copyright © CodeProject, 1999-2016
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