Advertisement
Guest User

Table Of Contents Of Android Activities using HashMap

a guest
Oct 21st, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. package net.superlinux.generalengineeringfree;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import android.R.integer;
  7. import android.app.Activity;
  8. import android.content.Intent;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.view.View.OnClickListener;
  12.  
  13. public class GeometryTableOfContents extends Activity {
  14.     /*
  15.      * Activity with a listing of Geometry shapes to get Area and Volume
  16.      * currently we have: 1- Circle 2- Rectangle 3- Triangle 4-
  17.      * This activity has ImageButtons whose view Ids are listed below in the variable table_of_contentsMap
  18.          * Clicking an ImageButton takes you to the corresponding Android Activity.
  19.      */
  20.     OnClickListener listener;
  21.    
  22.     public GeometryTableOfContents() {
  23.         // TODO Auto-generated constructor stub
  24.     }
  25.     Map <Integer,Class> table_of_contentsMap= new HashMap<Integer, Class>();
  26.     public void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.geometry_table_of_contents);
  29.        
  30.        
  31.         //table of contents classes. ImageButton view Id vs destination Activity
  32.         //E.g. GeometryRectangle.class represents an Android Activity class
  33.         table_of_contentsMap.put(R.id.geometry_rectangle_imgbutton, GeometryRectangle.class);
  34.         table_of_contentsMap.put(R.id.geometry_triangle_imgbutton, GeometryTriangle.class);
  35.         table_of_contentsMap.put(R.id.geometry_circle_imgbutton, GeometryCircle.class);
  36.         table_of_contentsMap.put(R.id.geometry_tarpizoid_imgbutton, GeometryTrapizoid.class);
  37.         table_of_contentsMap.put(R.id.geometry_sphere_imgbutton, GeometrySphere.class);
  38.         table_of_contentsMap.put(R.id.geometry_rectangular_solid_imgbutton, GeometryRectangularSolid.class);
  39.         table_of_contentsMap.put(R.id.geometry_cone_imgbutton, GeometryCone.class);
  40.         table_of_contentsMap.put(R.id.geometry_pyramid_imgbutton, GeometryPyramid.class);
  41.         table_of_contentsMap.put(R.id.geometry_prism_imgbutton, GeometryPrism.class);
  42.        
  43.  
  44.        
  45.         listener=new OnClickListener() {
  46.            
  47.             @Override
  48.             public void onClick(View v) {
  49.                 // TODO Auto-generated method stub
  50.                 if (!table_of_contentsMap.containsKey(v.getId())) return; //just in case, not to crash.
  51.                
  52.                 Intent intent = new Intent(GeometryTableOfContents.this, table_of_contentsMap.get(v.getId()));
  53.                 startActivity(intent);
  54.             }
  55.         };
  56.  
  57.         for (int view_id : table_of_contentsMap.keySet())   findViewById(view_id).setOnClickListener(listener);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement