Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 4.69 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Switch cases maximum implementation?
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.AdapterView;
  7. import android.widget.AdapterView.OnItemClickListener;
  8. import android.widget.ArrayAdapter;
  9. import android.widget.AutoCompleteTextView;
  10. import android.widget.Toast;
  11.  
  12. public class Search extends Activity
  13. {
  14.     public void onCreate(Bundle savedInstanceSate)
  15.     {
  16.         final AutoCompleteTextView autoComplete;
  17.         super.onCreate(savedInstanceSate);
  18.         setContentView(R.layout.searchshop);
  19.  
  20.         autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
  21.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, shops);
  22.         autoComplete.setAdapter(adapter);
  23.         autoComplete.setThreshold(1);
  24.         autoComplete.setOnItemClickListener(new OnItemClickListener()
  25.         {
  26.         @Override
  27.         public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
  28.         {
  29.             int index=999;
  30.             for(int i=0;i<shops.length;i++)
  31.             {
  32.                 if(autoComplete.getText().toString().trim().equals(shops[i]))
  33.                 {
  34.                     index=i;
  35.                     break;
  36.                 }
  37.             }
  38.  
  39.                 switch(index)
  40.                 {
  41.                 case 0:
  42.                     startActivity(new Intent(Search.this, Adidas.class));
  43.                     break;
  44.  
  45.                 case 1:
  46.                     startActivity(new Intent(Search.this, Affin.class));
  47.                     break;
  48.                 case 2:
  49.                     startActivity(new Intent(Search.this, AlamArt.class));
  50.                     break;
  51.                 case 3:
  52.                     startActivity(new Intent(Search.this, AlAmin.class));
  53.                     break;
  54.                 case 4:
  55.                     startActivity(new Intent(Search.this, Anakku.class));
  56.                     break;
  57.                 case 5:
  58.                     startActivity(new Intent(Search.this, Anggerik.class));
  59.                     break;
  60.                 case 6:
  61.                     startActivity(new Intent(Search.this, Asiari.class));
  62.                     break;
  63.                 case 7:
  64.                     startActivity(new Intent(Search.this, AsterSpring.class));
  65.                     break;
  66.                 case 8:
  67.                     startActivity(new Intent(Search.this, Audrey.class));
  68.                     break;
  69.                 case 9:
  70.                     startActivity(new Intent(Search.this, Badrul.class));
  71.                     break;
  72.                 case 10:
  73.                     startActivity(new Intent(Search.this, Badrul.class));
  74.                     break;
  75.                 case 11:
  76.                     startActivity(new Intent(Search.this, Badrul.class));
  77.                     break;
  78.                 default:
  79.                     Toast.makeText(Search.this, "Invalid Selection", Toast.LENGTH_SHORT).show();
  80.                 }
  81.             }
  82.             });
  83.  
  84.         }
  85. static final String[] shops = new String[]
  86.             {
  87.                 "Adidas", "Affin Bank ATM", "Alam Art Gallery", "Al Amin Kids", "Anakku", "Anggerik", "Asiari",
  88.                 "Aster Spring", "Audrey", "Badrul Songket", "Bata"};
  89. }
  90.        
  91. public class MyClass
  92. {
  93.     private static final Map<Integer, Class> LOOKUP =
  94.       new HashMap<Integer, Class>(...);
  95.     static
  96.     {
  97.       LOOKUP.put(0, Adidas.class);
  98.       LOOKUP.put(1, Affin.class);
  99.       ...
  100.     }
  101.  
  102.     public void onItemClick(...)
  103.     {
  104.       ...
  105.       // Replace switch statement with:
  106.       if (LOOKUP.containsKey(index))
  107.       {
  108.         startActivity(new Intent(Search.this, LOOKUP.get(index)));
  109.       }
  110.       else
  111.       {
  112.         Toast.makeText(Search.this,
  113.                        "Invalid Selection",
  114.                        Toast.LENGTH_SHORT).show();
  115.       }
  116.     }
  117.     ...
  118.   }
  119.        
  120. switch (calculation type)
  121. {
  122.    case: Fibonacci { ... }
  123.    case: Pithagoras { ... }
  124.    ...
  125.    case 104 : { ... }
  126. }
  127.        
  128. CalculationStrategy strategy = strategyFactor.getStrategy(calculation type);
  129. strategy.doCalculation;
  130.        
  131. Class[] myArray = new Class[...];
  132. myArray[0] = Adidas.class;
  133. //...
  134.  
  135. //instead of the switch
  136. startActivity(new Intent(Search.this, myArray[index]));
  137.        
  138. switch(FIRSTCHAR){
  139. case A: switch(index){
  140.         case 0: ..... break;
  141.         etc
  142.  
  143.         }
  144. break;
  145.  
  146. case B://do the same
  147. }
  148.        
  149. public interface Shop
  150. {
  151.     void startActivity(Intent i);
  152. }
  153.        
  154. public class Adidas implements Shop
  155. {
  156.     public Adidas(){
  157.         // ...
  158.     }
  159.  
  160.     public void startActivity(Intent i)
  161.     {
  162.         // specific implementation
  163.     }
  164. }
  165.        
  166. Shop[] shops = new Shop[]{ new Adidas(), new Affin(), ... };
  167.  
  168. for (Shop shop : shops)
  169. {
  170.    shop.startActivity(new Intent(Search.this));
  171. }