ANDROID: If ... Else as Include String

I am writing an Android application for work that shows the status of our phone lines, but it is neither here nor there.

I call one of our servers and get the JSON status text. Then I parse this by putting each row in a SortedMap (TreeMap), with the key being the name of the row and my own class as a value (which contains status and other data).

All of this works great.

When the application starts, it should show every line and information that I received, but nothing is updated.

JSON is returned and correctly added to the map.

This is a snapshot of code that does not work. I just iterate over the map and, depending on the key value, updates the corresponding TextView. The problem I am facing is that when it gets into the IF statement that matches it, it never runs this code. He skips it as if the values ​​do not match.

I do not see errors. This is the only way to do this, as I know that you cannot use Switch..Case, etc.

Can anyone see my mistake? I have been coding on Android for 1 week, so its probably a beginner's mistake!

Thanks neil

Iterator iterator = mapLines.entrySet().iterator();

while(iterator.hasNext())
{
// key=value separator this by Map.Entry to get key and value
Map.Entry<String, Status> mapEntry = (Map.Entry<String, Status>)iterator.next();

// getKey is used to get key of Map
String key = (String)mapEntry.getKey();

// getValue is used to get value of key in Map
Status value = (Status)mapEntry.getValue();

if(key == "Ski")
{
    TextView tvStatus = (TextView)findViewById(R.id.SkiStatus);

    tvStatus.setText(value.Status);
}
else if(key == "Cruise")
{
    TextView tvStatus = (TextView)findViewById(R.id.CruiseStatus);

    tvStatus.setText(value.Status);
}
else if(key == "Villas")
{
    TextView tvStatus = (TextView)findViewById(R.id.VillasStatus);

    tvStatus.setText(value.Status);
}
}
+3
source share
3 answers

equals() String Java. , String :

if (key.equals("Ski")) {
  ...
}

, NullPointerException, key null:

if ("Ski".equals(key)) {
  ...
}
+20

, , . , , , , .

, .

:

:

Map<String, Integer> textViews = new HashMap<String, Integer>();
textViews.put("Ski", R.id.Ski);
textViews.put("Cruise", R.id.Cruise);
textViews.put("Villas", R.id.Villas);

:

((TextView) findViewById(textViews.get(key))).setText(status);

if else, , .

+4

checking the key with the string literal "Ski", you can use as shown below. This will prevent the nullpointer exception.

if ("Ski".equals(key)) 
{ 
  ... 
} 
+1
source

Source: https://habr.com/ru/post/1767136/


All Articles