Saturday, 26 September 2015

Create a ListMenu for Android

Create a ListMenu for Android

unlike other classes our class for Listmenu will extend ListActivity not Activity

two methods need to be overrided here
1.onCreate like we did previously
2.onListItemClick

 Then we set up a String array (as instance varible bcz we want to use it in both the methods)
 String classes[]={"startingPoint","example1","example2","example3"};// we will name it exact same as the class name(case sensitive)

 we will not use any layout here,
 we will develop all in java here

 now we need to set setListAdapter(); // it takes a array adapter or a list adapter
 setListAdapter(new ArrayAdapter<String>(context,int,StringArray));//<> gives the type
 cotext= ClassName.this
 StringArray =classes
 int = android.R.layout.simple_list_item1

 for any of the list item when clicked it will call method onListItemClick

 WE need one more method now,
 3.onListItemClick
 the function has a parameter called position which will tell us about the item clicked
 we now set up a local string there
 String cheese= classes[position];// classes was our global string array containing the names of all the classes
 so now we can setup our class for intent according to the list item that was clicked

 Class ourclass- Class.forName("com.thenewboston.travis."+cheese);// will return the class name of particular class which has the intent filter in the manifest

 Intent ourIntent= new Intent(Menu.this, ourClass);
 startActivity(ourIntent);


 

No comments:

Post a Comment