简体   繁体   English

我的微调器出现问题

[英]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.

相关问题 我的手机上出现“将应用程序到应用程序的消息发送到屏幕上”的问题 - I've problems with “sinch app to app messaging” on my phone 我现在有一个例外 - I've got an exception at this point 我已经有三个星期的问题了。 我无法建立我的专案,因为这个问题 - I've been having problems for 3 weeks. I can't build my project because this problems 如果你点击按钮,我必须创造声音。 用我的音乐文件 - I've got to create sound if you click the button. With my music file 我的ContextMenu中有一个简单的错误,有人可以解决吗? - I've got a simple error in my ContextMenu, can someone please sort it out? 开发某些应用程序时,我的月食出错 - I got error on my eclipse when develop some apps 在Android中添加TrueTime库时出现错误 - I've got an error when I added the truetime library in android 在Fragment中使用ViewPager时,我有一个奇怪的错误 - I've got a weird bug when using a ViewPager inside a Fragment 我有两个列表,但只有一个itemclick方法 - I've got two list but one itemclick method Android TextAdventure开发期间发生了非常奇怪的崩溃 - I've got a very strange crash during Android TextAdventure development
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM