如何从我的sherlock ActionBar项目中启动一个Activity?

时间:2013-06-02 21:25:24

标签: eclipse android-activity android-actionbar actionbarsherlock menuitem

如何从我的sherlock ActionBar项目导航菜单中启动任何活动? 这是我的代码(我现在尝试使用吐司和功能)。

public class MainActivity extends SherlockActivity implements OnNavigationListener  {

private String[] pasti;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    pasti = getResources().getStringArray(R.array.Pasti);

    Context context = getSupportActionBar().getThemedContext();
    ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.Pasti, R.layout.sherlock_spinner_item);
    list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
    getSupportActionBar().setSubtitle("The Subtitle");

    getSupportActionBar().setDisplayShowTitleEnabled(true);

    getSupportActionBar().setDisplayShowCustomEnabled(false);

    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    getSupportActionBar().setListNavigationCallbacks(list, this);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

   getSupportActionBar().setDisplayUseLogoEnabled(true);

    getSupportActionBar().setDisplayShowHomeEnabled(true);   
}
 @Override
    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
     Toast toast=Toast.makeText(this,"Selected: " + pasti[itemPosition],Toast.LENGTH_LONG);
       toast.show();
       return true;
    }

}

` 请帮忙。感谢

1 个答案:

答案 0 :(得分:1)

我用这段代码解决了:

 switch (itemPosition) {
        case 1:
            Intent primi = new Intent();
            primi.setClass(getApplicationContext(), PrimiPiatti.class);
            startActivity(primi);
            break;
        case 2:
            break;
        case 3:
            break;

        }
        // return super.onOptionsItemSelected(itemPosition);
        return true;
    }
};
getSupportActionBar().setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);

}

@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    // TODO Auto-generated method stub
    return false;
}