I want to compare two Spinner values and, according to them, I want to display a result.
I have tried the following code, but everytime I run the application it does not show the expected result.
How to compare those values?
Here is the code
public class MyApp extends Activity implements OnItemSelectedListener {
Spinner temp1,temp2;
TextView t1,t2,t3;
EditText itemp;
Integer i,o,conv;
Double tc,tk,tf;
String sel1,sel2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
t1=(TextView)findViewById(R.id.t1);
t2=(TextView)findViewById(R.id.t2);
t3=(TextView)findViewById(R.id.otemp);
itemp=(EditText)findViewById(R.id.itemp);
temp1=(Spinner)findViewById(R.id.temp);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
temp1.setAdapter(adapter);
temp1.setOnItemSelectedListener(this);
temp2=(Spinner)findViewById(R.id.temp2);
ArrayAdapter<CharSequence> adapter2=ArrayAdapter.createFromResource(this,R.array.temperature,android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
temp2.setAdapter(adapter2);
temp2.setOnItemSelectedListener(this);
tk=Double.parseDouble(itemp.getText().toString());
if (sel1.equals("MyappVer") && sel2.equals("MYAPPVER")){
t3.setText("="+tk);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
Spinner temp1=(Spinner)parent;
Spinner temp2=(Spinner)parent;
if (temp1.getId()==R.id.temp) {
String item1 = parent.getItemAtPosition(position).toString();
t1.setText(item1);
sel1=item1;
}
if (temp2.getId()==R.id.temp2) {
String item2 = parent.getItemAtPosition(position).toString();
t2.setText(item2);
sel2=item2;
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
When I remove these lines the app works, but I need to compare those Spinner values
if (sel1.equals("MyappVer") && sel2.equals("MYAPPVER")){
t3.setText("="+tk);
}
Is something wrong with this implementation?
Logcat
08-04 14:48:28.651 13541-13541/com.combud.calcthree.compencalculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.combud.calcthree.compencalculator/com.combud.calcthree.compenc alculator.MyApp}: java.lang.NumberFormatException: Invalid double: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4507)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:248)
at java.lang.Double.parseDouble(Double.java:295)
at com.combud.calcthree.compencalculator.MyApp.onCreate(MyApp.java:50)
at android.app.Activity.performCreate(Activity.java:4469)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
at android.os.Handler.dispatchMessage(Handler.java:99)