我有一个问题。我创建了微调器,所有应用程序都运行而不会崩溃,但它不会改变int price的值,setText也不会工作
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button calculateBtn;
EditText userEcts;
TextView ectsPrice;
TextView summary;
Spinner courses;
String cors;
int price = 200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userEcts = (EditText) findViewById(R.id.ects_input);
summary = (TextView) findViewById(R.id.text_summary);
courses = (Spinner) findViewById(R.id.course_spinner);
calculateBtn = (Button) findViewById(R.id.calculate);
calculateBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//When the button is clicked, call the calculate method.
calculate();
}
});
final String[] courseArray = new String[4];
{
courseArray[0] = "Preddiplomski studij Menadžment";
courseArray[1] = "Preddiplomski studij Promet";
courseArray[2] = "Preddiplomski Upravni studij";
courseArray[3] = "Specijalistički studij Menadžment";
}
ArrayAdapter courseAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, courseArray);
courseAdapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
courses.setAdapter(courseAdapter);
courses.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//Get item from Spinner and store in string conductorSize........
cors = parent.getItemAtPosition(pos).toString();
if (cors.equals(0)) {
ectsPrice.setText("200");
price = 200;
} else if (cors.equals(1)) {
ectsPrice.setText("250");
price = 250;
} else if (cors.equals(2)) {
ectsPrice.setText("300");
price = 300;
} else if (cors.equals(3)) {
ectsPrice.setText("350");
price = 350;
} else {
//Do nothing
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
}
public void calculate() {
//gets entered text from the EditText,and convert to integers.
if (!TextUtils.isEmpty(userEcts.getText().toString())) {
Double ects = Double.parseDouble(userEcts.getText().toString());
//do the calculation
Double calculatedPrice = ects * price;
//set the value to the TextView, to display on screen.
summary.setText("Ukupno za platit: " + Double.toString(calculatedPrice) + "kn");
} else {
Toast.makeText(this, "Niste unijeli broj bodova", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View v) {
calculate();
}
}
因此,在选择位置0上的项目时,我想将文本设置为特定文本,并更改价格。有人可以帮忙吗?
答案 0 :(得分:0)
使用位置编号检查条件。
String[] courseArray = new String[4];
{
courseArray[0] = "Preddiplomski studij Menadžment";
courseArray[1] = "Preddiplomski studij Promet";
courseArray[2] = "Preddiplomski Upravni studij";
courseArray[3] = "Specijalistički studij Menadžment";
}
ArrayAdapter courseAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, courseArray);
courseAdapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
courses.setAdapter(courseAdapter);
courses.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//Get item from Spinner and store in string conductorSize........
cors = parent.getItemAtPosition(pos).toString();
if (cors.equals(courseArray[0])) {
ectsPrice.setText("200");
price = 200;
} else if (cors.equals(courseArray[1])) {
ectsPrice.setText("250");
price = 250;
} else if (cors.equals(courseArray[2])) {
ectsPrice.setText("300");
price = 300;
} else if (cors.equals(courseArray[3])) {
ectsPrice.setText("350");
price = 350;
} else {
//Do nothing
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
});
}
答案 1 :(得分:0)
使用开关声明:
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
switch(pos){
case 0:
ectsPrice.setText("200");
price = 200;
break;
case 1:
ectsPrice.setText("250");
price = 250;
break;
case 2:
ectsPrice.setText("300");
price = 300;
break;
case 3:
ectsPrice.setText("350");
price = 350;
break;
}
}