如何初始化autoComplete?我不能将它与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"
};
}
答案 0 :(得分:0)
将int autoComplete
修改为static
,final
等等并不重要,因为编译器抱怨您已经有一个名为“autoComplete”的变量这一事实。在您的实际代码示例中,您将AutoCompleteTextView
“autoCompletee”命名为两个与autoComplete
不同的e,以便它可以正常工作。但我建议使用更有意义的变量名称,如int autoCompleteValue
或类似的东西。无论哪种方式,问题是你有变量碰撞。一旦您在具有特定名称的范围内有变量,就不能再使用该名称......
答案 1 :(得分:-1)
你有autoComplete字段作为局部变量,需要设置为某个默认值。
只需设置final int autoComplete=0;
将其作为代码中的第三个语句移动,前两个语句应该是super ....和setContent(...)