按菜单按钮启动活动

时间:2014-03-18 13:10:18

标签: android menu

如何在按下按钮时让菜单启动活动?我的方式不起作用..出了什么问题?如果这样创建菜单。也许还有其他更好的方法?

public class MenuActivity extends Activity {


    private ListView MenuList;

    String classes[] = {"NewReviewActivity" };
    public void onCreate(Bundle menuinstance){
        super.onCreate(menuinstance);
        setContentView(R.layout.menu);

        //getActionBar().setDisplayHomeAsUpEnabled(true);

        MenuHelper menu_data[] = new MenuHelper[] {
                new MenuHelper(R.drawable.ic_menu_1, this.getString(R.string.menuItemTitle_1)),
                new MenuHelper(R.drawable.ic_menu_2, this.getString(R.string.menuItemTitle_2)),
                new MenuHelper(R.drawable.ic_menu_3, this.getString(R.string.menuItemTitle_3)),
                new MenuHelper(R.drawable.ic_menu_4, this.getString(R.string.menuItemTitle_4)),
                new MenuHelper(R.drawable.ic_menu_5, this.getString(R.string.menuItemTitle_5)),
                new MenuHelper(R.drawable.ic_menu_6, this.getString(R.string.menuItemTitle_6))
        };

        final MenuAdapter menuAdapter = new MenuAdapter(this, R.layout.menu_row, menu_data);


        MenuList = (ListView) findViewById(R.id.menuListView);
        MenuList.setAdapter(menuAdapter);
        MenuList.setOnItemSelectedListener(new  OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {

                String classchosen = classes[position];
                Class ourClass;
                try {
                    ourClass = Class.forName("com.project.simplify." + classchosen);
                    Intent ourIntent = new Intent(MenuActivity.this, ourClass);
                      startActivity(ourIntent);
                } catch (ClassNotFoundException e) {
                    System.out.print("Class not found");
                    e.printStackTrace();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // TODO Auto-generated method stub

            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.action_bar, menu);
        inflater.inflate(R.menu.main, menu);

        return super.onCreateOptionsMenu(menu);
    }

菜单助手类:

public class MenuHelper {
    public int icon;
    public String title;
    public MenuHelper(){
        super();
    }

    public MenuHelper(int icon, String title) {
        super();
        this.icon = icon;
        this.title = title;
    }
}

菜单适配器类:

public class MenuAdapter extends ArrayAdapter<MenuHelper>{

    Context context; 
    int layoutResourceId;    
    MenuHelper data[] = null;

    public MenuAdapter(Context context, int layoutResourceId, MenuHelper[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        MenuHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new MenuHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.menuItemIcon);
            holder.txtTitle = (TextView)row.findViewById(R.id.menuItemTitle);

            row.setTag(holder);
        }
        else
        {
            holder = (MenuHolder)row.getTag();
        }

        MenuHelper menu = data[position];
        holder.txtTitle.setText(menu.title);
        holder.imgIcon.setImageResource(menu.icon);

        return row;
    }

    static class MenuHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}

0 个答案:

没有答案