0

I would like to ask on how to write IF ELSE statement. My "if" is working fine, however, for the else, i couldn't get it working. I not sure what is working with the code.

if(weatherCode.equals("28"))
{
    mHandler.post(new Runnable()
    {
        @Override
        public void run()
        {
            // This gets executed on the UI thread so it can safely modify
            // Views

             image.setImageResource(R.drawable.logo);
        }
    });
}
else
{
  image.setImageResource(R.drawable.old);
}
6
  • Did you try with a weatherCode with value different than 28? Commented Jul 19, 2013 at 5:17
  • Hi, yes. But it didn't work. Commented Jul 19, 2013 at 5:19
  • Is it really String or Integer? Commented Jul 19, 2013 at 5:20
  • 1
    @Febbie is weatherCode value string or what? Commented Jul 19, 2013 at 5:21
  • 1
    why dont you add the mhandler for the else statement too? or is the else statement never executed? Commented Jul 19, 2013 at 5:23

4 Answers 4

2

Use thread as used in if part, as I added in comment too.

if(weatherCode.equals("28"))
{
mHandler.post(new Runnable()
{
    @Override
    public void run()
    {
        // This gets executed on the UI thread so it can safely modify
        // Views

         image.setImageResource(R.drawable.logo);
    }
});
}
else {
mHandler.post(new Runnable() {
    @Override
    public void run() {

        image.setImageResource(R.drawable.old);
    }
});
}
Sign up to request clarification or add additional context in comments.

6 Comments

@Febbie: Keep on accepting and up-voting answers and you will get more answers on your posts :)
thanks I would like to check with you, if I would like to do as else if is it like this? else if ( weatherCode.equals("15")) { mHandler.post(new Runnable() { public void run() { image.setImageResource(R.drawable.old); } }); } else { mHandler.post(new Runnable() { public void run() { image.setImageResource(R.drawable.new); } }); }
@Febbie: Out of curiosity, are you doing this in an AsyncTask?
yes just add the Handler and runnable for each else-if and else :)
@SiddharthLele Hi,Nope. I'm not
|
1

As i read the question the image is not being set correctly in the else part

UI actions need to be on the UI thread in the else case set the image in a new thread similar to previous action

else {
    mHandler.post(new Runnable() {
        @Override
        public void run() {

            image.setImageResource(R.drawable.old);
        }
    });
}

Comments

0
if (!weatherCode
        && weatherCode.equals("28")) {

    mHandler.post(new Runnable() {
        @Override
        public void run() {
            // This gets executed on the UI thread so it can safely
            // modify
            // Views

            image.setImageResource(R.drawable.logo);
        }
    });

} else {
    image.setImageResource(R.drawable.old);
}

Comments

0

I think the comment in your code provides already the right hint. In the if case you use a handler to make the image manipulation code run on the UI thread, but for your else case you don't.

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.