我正在使用一個將使用超過100個case聲明的開關箱。有沒有限制?開關櫃的最大實施?
案例的用法是針對我的AutoCompleteTextView,android教程的建議。
這裏是我的代碼的一部分,忽略Badrul.class他們將在以後更改。
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"};
}
我不知道是否還有更好的解決你的問題......你可以發佈一些代碼嗎? – davioooh 2012-01-27 09:58:46
只要確保沒有任何喜歡使用方法[OOP](http://en.wikipedia.org/wiki/Object-oriented_programming) – 2012-01-27 09:59:49
我不知道任何技術限制,將限制你有超過100 (除非你有數百萬個,在這種情況下,其他一些限制如內存,代碼大小等可能會生效)。但是,如果您有這麼多情況,可能值得質疑設計並尋找更好的選擇。在這種情況下,您可能想在switch語句中提供更多信息。作爲托馬斯說 – Thomas 2012-01-27 10:01:26