killerunicorn7
killerunicorn7

Reputation: 3

Android Menu Item Names

Okay, so I'm relatively new to Android, and I'm having some trouble with menu item names. I'll try to explain this the best I can.

public class Menu extends ListActivity{
    //Should be named the same as the Class itself 
    String classes[] = { "example1", "example2", "example3", "example4", "example5"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));

    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub  

        String classPosition = classes[position];

        super.onListItemClick(l, v, position, id);
        try{
        Class testClass = Class.forName("com.Test." + classPosition);
        Intent testIntent = new Intent(Menu.this, testClass);
        startActivity(testIntent);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
} 

I am currently using this method, but the problem I'm having, is that the name that is displayed on the menu has to be the same as the class name for it to work. If I wanted to call an activity named "Test" then the name displayed would have to be "Test". I'm sure I could figure this out if I had more time, but I just don't. Any help is appreciated. Thanks.

Upvotes: 0

Views: 454

Answers (1)

Yaqub Ahmad
Yaqub Ahmad

Reputation: 27659

Why don't you try the simplay way like:

private static final int MenuA = 1;
private static final int MenuB = 2;

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    { 
        menu.add(0,MenuA ,1,"Menu A"); 
        menu.add(0,MenuB ,1,"Menu B"); 
        return true; 
   }


   public boolean onOptionsItemSelected (MenuItem item)
   { 
    switch(item.getItemId())
    {
    case(MenuA):
    //your code
            break;
    case(MenuB):
    //your code
            break;
    }
    return false;
  }

Upvotes: 1

Related Questions