【问题标题】:i've got some problems with my spinner我的微调器有一些问题
【发布时间】:2010-09-03 16:30:22
【问题描述】:

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

这是我的代码:

public class Main extends 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) {

}

谢谢。

【问题讨论】:

    标签: android arrays spinner


    【解决方案1】:

    代替:

    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;
    

    【讨论】:

    • 我不知道为什么,但是当我这样做时,textView 描述中有一个城市的单个字符串,但这只是一个,它适用于我选择的所有项目。 ://
    • 好的,谢谢,这是缺少的休息时间。谢谢;)
    【解决方案2】:

    您需要在每个案例之后添加breaks,否则匹配时将继续执行所有案例:

        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】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 2011-07-20
        • 2020-05-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多