0

I created a simple button that sets an EditText when clicked. I created another method called GetTime() which should check a spinner for a string, and then set an Time variable. Then my onClick() just sets the EditText to the value of Time. Its very simple code. The problem is when I do if TimerSpin.equals("5 Mins"), it never changes the value of time. It's still 0. So I'm not sure if my spinner is being checked correctly. But it prints values to the EditText just fine. The spinner is just a StringArray I created in my Strings.xml.

package com.Alan.Gym_Rat;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.view.*;
import android.view.View.OnClickListener;

public class MainScreen extends Activity implements OnClickListener
{
int Calories = 0;
int Time = 0;
EditText CalText;
Button Calculate;
Spinner TimerSpin;
Spinner ExerSpin;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);

    TextView MainTitle = (TextView) findViewById(R.id.Title);  
    Typeface Molot = Typeface.createFromAsset(getAssets(), "Molot.otf"); 
    MainTitle.setTypeface(Molot);

    ExerSpin = (Spinner)findViewById(R.id.ExerciseSpin);
    TimerSpin = (Spinner)findViewById(R.id.TimeSpin);

    Calculate =(Button)findViewById(R.id.Calc);
    CalText = (EditText)findViewById(R.id.CalcText);

    Calculate.setOnClickListener((OnClickListener)this);

}
public void GetTime()
{
    if(TimerSpin.equals("5 Mins"))
    {
        Time = 5;
    }
    else
    {
        Time = 0;
    }
}

public void onClick(View v) 
{
    if(v.getId() == R.id.Calc)
    {
        GetTime();
        CalText.setText(Time + " Calories");
    }

}
}
2
  • what is the issue? post error code/ Commented Feb 15, 2012 at 6:35
  • I don't get any errors. It just always sets the value of Time = 0, and prints 0 in the EditText. But I want it to set Time = 5 if Spinner.equals("5 Mins") Commented Feb 15, 2012 at 6:38

1 Answer 1

3

The problem is, you are comparing Spinner to a String which actually should be Spinner's selected String to another String..

correct this by:

if(TimerSpin.getSelectedItem().toString().equals("5 Mins"))
{
   // your code...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.