切换案例最大化实施? [英] Switch cases maximum implementation?
问题描述
我正在使用单个开关案例,其中将使用超过100个案例语句。是否有任何限制?
I am using a single switch cases which will have more than 100 cases statement to be used. Are there any limit ?
案例的使用是针对我的AutoCompleteTextView,android教程的建议。
The usage of cases are for the suggestions of my AutoCompleteTextView, android tutorial.
以下是我的代码的一部分,忽略Badrul.class,稍后将更改它们。
Here are part of my codes, ignore the Badrul.class they will be changed later.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;
public class Search extends Activity
{
public void onCreate(Bundle savedInstanceSate)
{
final AutoCompleteTextView autoComplete;
super.onCreate(savedInstanceSate);
setContentView(R.layout.searchshop);
autoComplete = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, shops);
autoComplete.setAdapter(adapter);
autoComplete.setThreshold(1);
autoComplete.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
int index=999;
for(int i=0;i<shops.length;i++)
{
if(autoComplete.getText().toString().trim().equals(shops[i]))
{
index=i;
break;
}
}
switch(index)
{
case 0:
startActivity(new Intent(Search.this, Adidas.class));
break;
case 1:
startActivity(new Intent(Search.this, Affin.class));
break;
case 2:
startActivity(new Intent(Search.this, AlamArt.class));
break;
case 3:
startActivity(new Intent(Search.this, AlAmin.class));
break;
case 4:
startActivity(new Intent(Search.this, Anakku.class));
break;
case 5:
startActivity(new Intent(Search.this, Anggerik.class));
break;
case 6:
startActivity(new Intent(Search.this, Asiari.class));
break;
case 7:
startActivity(new Intent(Search.this, AsterSpring.class));
break;
case 8:
startActivity(new Intent(Search.this, Audrey.class));
break;
case 9:
startActivity(new Intent(Search.this, Badrul.class));
break;
case 10:
startActivity(new Intent(Search.this, Badrul.class));
break;
case 11:
startActivity(new Intent(Search.this, Badrul.class));
break;
default:
Toast.makeText(Search.this, "Invalid Selection", Toast.LENGTH_SHORT).show();
}
}
});
}
static final String[] shops = new String[]
{
"Adidas", "Affin Bank ATM", "Alam Art Gallery", "Al Amin Kids", "Anakku", "Anggerik", "Asiari",
"Aster Spring", "Audrey", "Badrul Songket", "Bata"};
}
推荐答案
以前代码将无法管理你达到了Java所施加的任何限制。
The code will become unmanageable before you hit any limit that Java imposes.
您是否考虑过重构代码?根据switch语句的设计目标,您可以:
Have you considered refactoring the code? Depending on what the switch statement is designed to achieve you could either:
- Use a map to provide different results
- Create a hierarchy of simple objects that give the behaviour you require for each case
所以在你的情况下,你最好定义一个静态 Map
的索引值为类
:
So in your case, you would be better off defining a static Map
of index values to Classes
:
public class MyClass
{
private static final Map<Integer, Class> LOOKUP =
new HashMap<Integer, Class>(...);
static
{
LOOKUP.put(0, Adidas.class);
LOOKUP.put(1, Affin.class);
...
}
public void onItemClick(...)
{
...
// Replace switch statement with:
if (LOOKUP.containsKey(index))
{
startActivity(new Intent(Search.this, LOOKUP.get(index)));
}
else
{
Toast.makeText(Search.this,
"Invalid Selection",
Toast.LENGTH_SHORT).show();
}
}
...
}
这使得 onItemClick()
中的代码更易于阅读。您可以更进一步,定义一个私有的 startActivity()
方法,该方法使用索引并包含所有switch语句替换代码。
This makes the code in onItemClick()
easier to read. You could go one step further and define a private startActivity()
method that takes the index to be used and contains all the switch statement replacement code.
这篇关于切换案例最大化实施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!