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 {
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 .
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:
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;
}
Try using description.setText(CharSequence text)
instead of append. Append is for appending text.
精彩评论