[英]i've got some problems with my spinner
So i'm starting a application and the first thing to do is make a description of a city when the city is selected. 因此,我正在启动一个应用程序,并且要做的第一件事是在选择城市时对城市进行描述。 I can show the description but it make the description of all the cities on the same time and it don't come out when i select another city : it add more and more .
我可以显示描述,但是它同时描述所有城市,当我选择另一个城市时它不会出现:它增加的越来越多。
this is my code : 这是我的代码:
public class Main extends Activity implements OnItemClickListener, OnItemSelectedListener { 公共类Main Extended Activity实现OnItemClickListener,OnItemSelectedListener {
TextView description;
Spinner spin;
ArrayAdapter adapter_city;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
description = (TextView)findViewById(R.id.description);
spin = (Spinner)findViewById(R.id.spin);
adapter_city = ArrayAdapter.createFromResource(this, R.array.Cities, android.R.layout.simple_spinner_item);
adapter_city.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter_city);
spin.setOnItemSelectedListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
switch(position){
case 0 : description.append(getString(R.string.Paris));
case 1 : description.append(getString(R.string.Chicago));
case 2 : description.append(getString(R.string.NewYork));
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
thank you . 谢谢 。
Try using description.setText(CharSequence text)
instead of append. 尝试使用
description.setText(CharSequence text)
而不是追加。 Append is for appending text. 追加用于追加文本。
Instead of: 代替:
description.append(getString(R.string.Paris));
Why don't you just use: 您为什么不只使用:
description.setText(R.string.Paris);
Here is what it should look like: 它应该是这样的:
case 0 : description.setText(R.string.Paris);
break;
case 1 : description.setText(R.string.Chicago);
break;
case 2 : description.setText(R.string.NewYork);
break;
You need to add break
s after every case or execution will continue to all of them when matched: 您需要在每种情况后添加
break
,否则匹配时将继续执行所有break
:
switch(position){
case 0 : description.setText(R.string.Paris); break;
case 1 : description.setText(R.string.Chicago); break;
case 2 : description.setText(R.string.NewYork); break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.