views:

39

answers:

3

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 .

A: 

Try using description.setText(CharSequence text) instead of append. Append is for appending text.

Robert
A: 

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;
magicman
i don't know why but when i do that there is this single string for a city in the textView descrption but this is just one and it works with all the item i select . :/
Tsunaze
Ok thank you it was the break which was missing. Thank you ;)
Tsunaze
if an answer works for you, feel free to mark it correct by clicking the check mark next to my answer. You accept rate is not very high, you might want to go to your past questions and mark answers as correct.
magicman
A: 

You need to add breaks 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;
}
antonyt