Click here to Skip to main content
15,881,579 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Java
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
Updated 27-Nov-12 5: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?
[no name] 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

I think I get what you're trying to do now. In that case, I think you'd want something like this:

Java
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:
Java
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.
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900