7

我想在我的activity下面的action bar中使用spinner是onCreateOptionsMenu:我使用这个教程来实现这个方法。我的问题是当活动是午餐时,onNavigationItemSelected 方法会触发并且开关/案例上的代码运行以及我为位置 0 设置的活动打开。当活动是午餐时,我应该怎么做才能防止运行开关/机箱?

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getSupportMenuInflater().inflate(R.menu.main, menu);

    SpinnerAdapter mSpinnerAdapter;
    if(Build.VERSION.SDK_INT <= 10)
    {
        mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_item);
    }
    else
    {
        mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_dropdown_item);
    }
    ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener()
    {
        @Override
        public boolean onNavigationItemSelected(int position, long itemId)
        {
            switch (position)
            {
                case 0:
                    Intent searchIntent = new Intent(ActivitySearchBusiness.this, ActivityFindBusinessCity.class);
                    startActivity(searchIntent);
                    break;
                case 2:
                    Intent dealsIntent = new Intent(ActivitySearchBusiness.this, ActivityDeals.class);
                    startActivity(dealsIntent);
                    break;
                case 3:
                    Intent eventsIntent = new Intent(ActivitySearchBusiness.this, ActivityEvents.class);
                    startActivity(eventsIntent);
                    break;
            }

            return true;
        }
    };
    actionBar.setListNavigationCallbacks(mSpinnerAdapter, 

    return super.onCreateOptionsMenu(menu);
}
4

1 回答 1

6

您不需要里面的以下代码onCreateOptionsMenu(Menu)

…………

删除它并将其放在您的活动onCreate(Bundle)方法中。

编辑:

声明一个全局boolean变量:

boolean initializing = true;

将以下代码放入onCreate(Bundle)

SpinnerAdapter mSpinnerAdapter;
if(Build.VERSION.SDK_INT <= 10)
{
    mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_item);
}
else
{
    mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.spinner_data,android.R.layout.simple_spinner_dropdown_item);
}
ActionBar.OnNavigationListener mOnNavigationListener = new ActionBar.OnNavigationListener()
{
    @Override
    public boolean onNavigationItemSelected(int position, long itemId)
    {

        if (initializing) {
            initializing = false;
        } else {             
            switch (position)
            {
                case 0:
                    Intent searchIntent = new Intent(ActivitySearchBusiness.this, ActivityFindBusinessCity.class);
                    startActivity(searchIntent);
                    break;
                case 2:
                    Intent dealsIntent = new Intent(ActivitySearchBusiness.this, ActivityDeals.class);
                    startActivity(dealsIntent);
                    break;
                case 3:
                    Intent eventsIntent = new Intent(ActivitySearchBusiness.this, ActivityEvents.class);
                    startActivity(eventsIntent);
                    break;
            }
        }

        return true;
    }
};
//actionBar.setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
getActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
于 2013-09-01T20:07:34.987 回答