Android,旋转器未更新
Android, spinner not updating
我有一个可以使用的微调器。但是,当我尝试更改 selection 时,它不会更新。
这是应用程序原则:您 select 一个地点,例如巴塞罗那,您应该看到价格,例如 30 欧元。
但是,正如我所解释的,如果您将位置更改为其他位置,价格不会改变。
这是代码(一些位是西班牙语):
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 0){
priceAdulto = 34.00;
priceNino = 28.00;
setContentView(R.layout.activity_my);
TextView textViewAdulto = (TextView) findViewById(R.id.textView3);
textViewAdulto.setText(priceAdulto.toString());
TextView textViewNino = (TextView) findViewById(R.id.textView4);
textViewNino.setText(priceNino.toString());
Toast.makeText(getApplicationContext(), "Updated",
Toast.LENGTH_SHORT).show();
}
if (pos == 1){
priceAdulto = 25.00;
priceNino = 22.00;
setContentView(R.layout.activity_my);
TextView textViewAdulto = (TextView) findViewById(R.id.textView3);
textViewAdulto.setText(priceAdulto.toString());
setContentView(R.layout.activity_my);
TextView textViewNino = (TextView) findViewById(R.id.textView4);
textViewNino.setText(priceNino.toString());
Toast.makeText(getApplicationContext(), "Updated",
Toast.LENGTH_SHORT).show();
}
if (pos == 2) {
priceAdulto = 43.50;
priceNino = 39.00;
setContentView(R.layout.activity_my);
TextView textViewAdulto = (TextView) findViewById(R.id.textView3);
textViewAdulto.setText(priceAdulto.toString());
setContentView(R.layout.activity_my);
TextView textViewNino = (TextView) findViewById(R.id.textView4);
textViewNino.setText(priceNino.toString());
Toast.makeText(getApplicationContext(), "Updated",
Toast.LENGTH_SHORT).show();
}
您的错误在于连续设置 ContentView。
这会将视图重置为其初始状态。
您应该只设置一次布局。
可能在 onCreate 中(如果您在 Activity 中)或在 onCreateView 中(如果您在 Fragment 中)。
此外,由于您使用的是数字,执行多个条件检查的更优雅的方法是使用 switch(pos){...}
只设置一次内容视图,多次设置会回到开头。在创建时使用设置内容视图,仅此而已。
希望有用。
我有一个可以使用的微调器。但是,当我尝试更改 selection 时,它不会更新。
这是应用程序原则:您 select 一个地点,例如巴塞罗那,您应该看到价格,例如 30 欧元。
但是,正如我所解释的,如果您将位置更改为其他位置,价格不会改变。
这是代码(一些位是西班牙语):
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 0){
priceAdulto = 34.00;
priceNino = 28.00;
setContentView(R.layout.activity_my);
TextView textViewAdulto = (TextView) findViewById(R.id.textView3);
textViewAdulto.setText(priceAdulto.toString());
TextView textViewNino = (TextView) findViewById(R.id.textView4);
textViewNino.setText(priceNino.toString());
Toast.makeText(getApplicationContext(), "Updated",
Toast.LENGTH_SHORT).show();
}
if (pos == 1){
priceAdulto = 25.00;
priceNino = 22.00;
setContentView(R.layout.activity_my);
TextView textViewAdulto = (TextView) findViewById(R.id.textView3);
textViewAdulto.setText(priceAdulto.toString());
setContentView(R.layout.activity_my);
TextView textViewNino = (TextView) findViewById(R.id.textView4);
textViewNino.setText(priceNino.toString());
Toast.makeText(getApplicationContext(), "Updated",
Toast.LENGTH_SHORT).show();
}
if (pos == 2) {
priceAdulto = 43.50;
priceNino = 39.00;
setContentView(R.layout.activity_my);
TextView textViewAdulto = (TextView) findViewById(R.id.textView3);
textViewAdulto.setText(priceAdulto.toString());
setContentView(R.layout.activity_my);
TextView textViewNino = (TextView) findViewById(R.id.textView4);
textViewNino.setText(priceNino.toString());
Toast.makeText(getApplicationContext(), "Updated",
Toast.LENGTH_SHORT).show();
}
您的错误在于连续设置 ContentView。
这会将视图重置为其初始状态。
您应该只设置一次布局。
可能在 onCreate 中(如果您在 Activity 中)或在 onCreateView 中(如果您在 Fragment 中)。
此外,由于您使用的是数字,执行多个条件检查的更优雅的方法是使用 switch(pos){...}
只设置一次内容视图,多次设置会回到开头。在创建时使用设置内容视图,仅此而已。 希望有用。