【发布时间】:2012-01-27 02:01:16
【问题描述】:
如何初始化自动完成?我不能将它与 AutoCompleteTextView 一起使用,因为它会告诉我局部变量是重复的。也尝试将其声明为静态,但不允许这样做。
请指教!
public class Search extends Activity {
public void onCreate(Bundle savedInstanceSate) {
final int autoComplete;
super.onCreate(savedInstanceSate);
setContentView(R.layout.searchshop);
//The duplicate im talking about
AutoCompleteTextView autoCompletee = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, shops);
autoCompletee.setAdapter(adapter);
autoCompletee.setThreshold(1);
autoCompletee.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Intent intent;
int index=999;
for(int i=0;i<shops.length;i++) {
//The local variable autoComplete may not been initialized
if(shops[i].equals(Integer.toString(autoComplete))) {
index=i;
break;
}
}
switch(index) {
case 0:
intent=new Intent(Search.this, Adidas.class);
startActivity(intent);
break;
case 1:
intent=new Intent(Search.this, Affin.class);
startActivity(intent);
break;
}
}
});
}
static final String[] shops = new String[] {
"Adidas", "Affin Bank", "Alam Art", "Al Amin"
};
}
【问题讨论】:
-
这毫无意义。您有一个名为 autoComplete 的 int 变量,它从未分配过任何值。所以当然它没有被初始化。你有什么问题?
-
答案很大程度上取决于您希望
autoComplete具有什么价值。 -
问题不是如何,而是为什么。据我所知,除了一个
equals调用外,没有使用该变量,该调用始终返回 false。 -
抱歉,我是新手。以前 autoComplete 是使用 AutoCompleteTextView 初始化的,但这会导致“无法在以不同方法定义的内部类中引用非最终变量 autoComplete”。一旦我用 final 声明 autoComplete,它就会告诉我我有变量重复。有没有人可以过来看看? stackoverflow.com/questions/8997684/…