Android菜单项列表导航

时间:2013-08-13 07:52:09

标签: android menu

我需要帮助,请有人帮助我切换和案例......

我在操作栏item1,item2,item3中有3个项目,我有3个活动item1Activity.java,item2Activity,item2Activity ..我想在选择项目时从菜单调用这些活动..

public class MainActivity extends Activity {


/** An array of strings to populate dropdown list */
String[] actions = new String[] {
    "Item1",
    "Item2",
    "Item3"
};
protected int position;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    /** Create an array adapter to populate dropdownlist */
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, actions);


    /** Enabling dropdown list navigation for the action bar */
    getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

    /** Defining Navigation listener */
    ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {

        @Override
        public boolean onNavigationItemSelected(int itemPosition, long itemId) {

            switch (itemPosition) {
            case 1:
                Intent i = new Intent();
                i.setClass(getApplicationContext(), Item1Activity.class);
                startActivity(i);
                return true;

            case 2:
                Intent i = new Intent();
                i.setClass(getApplicationContext(), Item2Activity.class);
                startActivity(i);
                return true;
                }

                }
        };

    /** Setting dropdown items and item navigation listener for the actionbar */
    getActionBar().setListNavigationCallbacks(adapter, navigationListener);  

}

}

1 个答案:

答案 0 :(得分:0)

  

使用MainActivity.this而不是getApplicationContext()

活动是上下文的孩子