convertto.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View v1,
int index1, long arg3) {
//from meters
var1=Double.parseDouble(lvalue.getText().toString());
var2=var1;
if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Meter") ))
{
Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();
var2=var1;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Inches") && (s1.equalsIgnoreCase("Meter")))
{
Toast.makeText(v1.getContext(), " to inches",Toast.LENGTH_SHORT).show();
var2=var1 * 39.3700787;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Feet") && (s1.equalsIgnoreCase("Meter")))
{
Toast.makeText(v1.getContext(), " to Feet",Toast.LENGTH_SHORT).show();
var2=var1 * 3.281;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Yards") && (s1.equalsIgnoreCase("Meter")))
{
Toast.makeText(v1.getContext(), " to Yards",Toast.LENGTH_SHORT).show();
var2=var1 * 1.0936133 ;
lresult.setText("" + var2);
}
}
}
//from inches
if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Inches")))
{
Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();
var2=var1/39.3700787;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Inches") && (s1.equalsIgnoreCase("Inches")))
{
Toast.makeText(v1.getContext(), " to inches",Toast.LENGTH_SHORT).show();
var2=var1;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Feet") && (s1.equalsIgnoreCase("Inches")))
{
Toast.makeText(v1.getContext(), " to Feet",Toast.LENGTH_SHORT).show();
var2=var1/12;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Yards") && (s1.equalsIgnoreCase("Inches")))
{
Toast.makeText(v1.getContext(), " to Yards",Toast.LENGTH_SHORT).show();
var2 = var1/36;
lresult.setText("" + var2);
}
}
}
//from feet
if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Feet")))
{
Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();
var2=var1/3.281;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Inches") && (s1.equalsIgnoreCase("Feet")))
{
Toast.makeText(v1.getContext(), " to inches",Toast.LENGTH_SHORT).show();
var2=var1*12;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Feet") && (s1.equalsIgnoreCase("Feet")))
{
Toast.makeText(v1.getContext(), " to Feet",Toast.LENGTH_SHORT).show();
var2=var1;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Yards") && (s1.equalsIgnoreCase("Feet")))
{
Toast.makeText(v1.getContext(), " to Yards",Toast.LENGTH_SHORT).show();
var2 = var1/3;
lresult.setText("" + var2);
}
}
}
//from yards
if ((quantity[index1]=="Meter") && (s1.equalsIgnoreCase("Yards")))
{
Toast.makeText(v1.getContext(), " to meter",Toast.LENGTH_SHORT).show();
var2=var1/1.0936133;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Inches") && (s1.equalsIgnoreCase("Yards")))
{
Toast.makeText(v1.getContext(), " to inches",Toast.LENGTH_SHORT).show();
var2=var1*36;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Feet") && (s1.equalsIgnoreCase("Yards")))
{
Toast.makeText(v1.getContext(), " to Feet",Toast.LENGTH_SHORT).show();
var2=var1*3;
lresult.setText("" + var2);
}
else{
if ((quantity[index1]=="Yards") && (s1.equalsIgnoreCase("Yards")))
{
Toast.makeText(v1.getContext(), " to Yards",Toast.LENGTH_SHORT).show();
var2 = var1;
lresult.setText("" + var2);
}
}
}
}
}
}
}
}
答案 0 :(得分:1)
这是因为当您第一次执行您的应用并且onItemSelected
被解雇时。要摆脱这种情况,你需要自己应用一些逻辑内容,以便当Activity首次启动时,它不会在onItemSelected
内部启动代码,并且在用户进行选择时起作用。 / p>
你可以做点什么,
int spinner_counter = 1, spinnner_selected = 0; // declare globally
@Override
public void onItemSelected(AdapterView<?> arg0, View v1,
int index1, long arg3) {
if(spinnner_selected < spinner_counter){
spinnner_selected++;
}
else{
//execute your code here
}
}