我在公共类中声明了一个变量,然后为该变量分配了if语句,但是在我计算时并没有从if语句获取值。怎么解决呢? 这里的完整代码https://pastebin.com/yuaMx4Mk谢谢
double doubleJarak=Double.parseDouble(jarak);
double doublewaktu=Double.parseDouble(waktu);
//SPinner Code
MaterialSpinner spinner = (MaterialSpinner) findViewById(R.id.spinner);
spinner.setItems("Premium Rp 7.000", "Pertalite Rp 7.650", "Pertamax Rp 9.850", "Pertamax Turbo Rp 11.200");
spinner.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
@Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
//Snackbar.make(view, "Clicked " + item, Snackbar.LENGTH_LONG).show();
if(position==0)
hargaBBM=7000;
else if(position==1)
hargaBBM=7650;
else if(position==2)
hargaBBM=9850;
else
hargaBBM=11200;
}
});
MaterialSpinner spinner2 = (MaterialSpinner) findViewById(R.id.spinner2);
spinner2.setItems("Honda", "Yamaha", "Kawasaki", "Suzuki", "Lainnya");
spinner2.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
@Override public void onItemSelected(MaterialSpinner view, int position2, long id, String item) {
//Snackbar.make(view, "Clicked " + item + position2, Snackbar.LENGTH_LONG).show();
if (position2==0){
MaterialSpinner spinner3 = (MaterialSpinner) findViewById(R.id.spinner3);
spinner3.setItems("Revo 110 FI", "Blade 125 FI", "Supra X 125 FI");
spinner3.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
@Override public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
//Snackbar.make(view, "Clicked " + item, Snackbar.LENGTH_LONG).show();
if(position==0){
iritHonda=62200;
}else if(position==1){
iritHonda=61800;
}else if(position==2){
iritHonda=61800;
}
}
});
}
}
});
totalHarga=iritHonda*hargaBBM;
TextView Harga=(TextView)findViewById(R.id.totalHarga);
Harga.setText(Double.toString(totalHarga));
}
}
答案 0 :(得分:1)
您的问题是这些函数是回调。选择项目后,将异步调用它们。它们不返回值。在实际调用它们之前,不能使用它们设置的变量。这意味着所有在onCreate末尾使用它们的代码都位于错误的位置,需要在两个微调程序的处理程序内部调用它(因此,当任何一个微调程序更改时,它都可以更改)。