-1

I am facing a very strange problem. I have written the below code.

    if (verifiedStatus.equals("1")) {
    imageView.setImageResource(R.drawable.ic_verified_user_black_24dp);
    } 
    else if (verifiedStatus.equals("0")) {
    imageView.setImageResource(R.drawable.ic_not_interested_black_24dp);
    } else {}

As the code runs, if the value is 1, first the verified image comes but later it changes to the not interested image. I tried debugging and I saw that the code goes into an internal file/code and in the below lines, it changes the image to the other image.

for (;;) {
    Message msg = queue.next(); // might block
    if (msg == null) {
        // No message indicates that the message queue is quitting.
        return;
    }

Can someone help me in resolving this issue?

Sid Chopra
  • 19
  • 4
  • Is there a reason you use a string rather than a enum, int, or boolean? – OneCricketeer Nov 17 '17 at 00:00
  • The already existing answers are not of any help. I used == first and then changed it to .equals with the same result. – Sid Chopra Nov 17 '17 at 00:20
  • No, I mean nothing specific. The server returns a 0/1 in JSON format. So, I extract the value using the key. I used getstring .. so I compare it as a string. – Sid Chopra Nov 17 '17 at 00:22
  • JSON has integer and boolean values. You don't need to just use whatever the JSON gives to compare against. `==` is never how you want to compare the string content, so "not executed correctly" is what you're getting. Without a [mcve] of the full problem, we can't see the actual error or your expectation – OneCricketeer Nov 17 '17 at 01:24

2 Answers2

2

you must do like this:

if (verifiedStatus.equals("1")) {
    imageView.setImageResource(R.drawable.ic_verified_user_black_24dp);
    } 
    else if (verifiedStatus.equals("0") {
    imageView.setImageResource(R.drawable.ic_not_interested_black_24dp);
    } else {}
A Farmanbar
  • 4,381
  • 5
  • 24
  • 42
0

First of all, you don't need two nested if else if else. Do it like this if else if or if else statements.

if (verifiedStatus.equals("1")) {
  imageView.setImageResource(R.drawable.ic_verified_user_black_24dp);
} 
else if (verifiedStatus.equals("0")) {
 imageView.setImageResource(R.drawable.ic_not_interested_black_24dp);
}

or

if (verifiedStatus.equals("1")) {
  imageView.setImageResource(R.drawable.ic_verified_user_black_24dp);
} 
else {
 imageView.setImageResource(R.drawable.ic_not_interested_black_24dp);
}

or use switch statments, case 1: ..do something..break, case 2: ..do something.. break Also, you need to return something with the return statement.

One main thing about your question, it doesn't give all the information so please edit your question if my answer won't be helpful to get help from others in the community.

Tayyab
  • 47
  • 1
  • 2
  • 8
  • I think only if + else if doesn't work since compiler wants to know what to do if both conditions are unmet. Also, I used if and else only at first .. but since there was an issue, I changed it around and that's why the above code. Now its back to if + else and the result is identical. Switch is doing the same thing too. Is return mandatory? And what if I dont want to return anything? Should I return null? Also, I am not sure what additional information will be useful here? Its a pretty long project .. cant paste everything. – Sid Chopra Nov 16 '17 at 23:56
  • This is still wrong with `==` – OneCricketeer Nov 16 '17 at 23:59
  • I edited my answer so check it if works. You cannot compare strings with == to comparator. you have use the method like this `someString.equals(someOtherString)`. check here https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java About the return statement if you use the keyword you have to return something,`null` you only return when you need to return null, the same like if you need to return any other thing. and the last else statement you are talking, if you want to use it you have to put in something that would be the default output. otherwise remove it. – Tayyab Nov 17 '17 at 00:08
  • Done all (.equals, return null (as I have to just set a variable value), no last statement (only if + else)). Same result. – Sid Chopra Nov 17 '17 at 00:16
  • I that case edit your questions edit your question and give full details so we can understand what do you want the program to do and how you are implementing it. – Tayyab Nov 17 '17 at 15:53