Tom Fisher Tom Fisher - 6 months ago 17
JSON Question

Android Switch statement that show an image depending what is pulled from JSON

so currently have a mini switch statement that I need help with. So I have a json string that I then pull out a type from the json e.g light and then need to have a switch statement that can then set the different "types" to show different images.

having never dealt with json in switches I'm not sure what to do!

String device = jsonObject.getString("type");
switch (device) {
case 0:
device.equalsIgnoreCase("Light");
cell.typeImg.setImageResource(R.mipmap.ic_lights_on);
break;
case 1:
device.equalsIgnoreCase("Lock";
cell.typeImg.setImageResource(R.mipmap.ic_lock_open_black_24dp);
break;
}

Answer
switch (device)
{ 
    case "Light":
       //do stuff for light
       break;
    case "Lock":
       //do stuff for lock
       break;

}

cases should be possible values of the value inside the switch statement