代码之家  ›  专栏  ›  技术社区  ›  Tsunaze

我的旋转器出了点问题

  •  0
  • Tsunaze  · 技术社区  · 16 年前

    所以我正在启动一个应用程序,首先要做的是在选择城市时对城市进行描述。我可以显示描述,但它同时对所有城市进行描述,并且当我选择另一个城市时不会显示出来:它会添加越来越多的内容。

    这是我的代码:

    公共类主扩展活动实现OnItemLickListener、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) {
    
    }
    

    谢谢您。

    3 回复  |  直到 14 年前
        1
  •  0
  •   magicman    16 年前

    而不是:

    description.append(getString(R.string.Paris));
    

    为什么不使用:

    description.setText(R.string.Paris);
    

    它应该是这样的:

    case 0 :    description.setText(R.string.Paris);
                break;
    
    case 1 :    description.setText(R.string.Chicago);
                break;
    
    case 2 :    description.setText(R.string.NewYork);
                break;
    
        2
  •  0
  •   antonyt    16 年前

    你需要添加 break s在每一个案件或执行之后,匹配后将继续对所有案件或执行:

        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;
    }
    
        3
  •  0
  •   Andro Selva Anand Wadhwani    14 年前

    试用使用 description.setText(CharSequence text) 而不是追加。append用于附加文本。