Hey all I am wondering how I can have a generic call to one of my classes so that I can minimize my code.
Currently I have a at of if else code that determines which class should be loaded up.
As an example in my MainActivity:
if (btnName.equals("up"))
if (globalRoom.equals("ABC")) {
btnNumber = 0;
ABC.sendTheCmd("?action=volume_up", globalTV, all_TvType, btnNumber, false);
}
else if (globalRoom.equals("NBC")) {
btnNumber = 1;
NBC.sendTheCmd("?action=volume_up", globalTV, all_TvType, btnNumber, false);
}
else if (globalRoom.equals("CBS")) {
btnNumber = 2;
CBS.sendTheCmd("?action=volume_up", globalTV, all_TvType, btnNumber, false);
}
else if (globalRoom.equals("REK")) {
btnNumber = 3;
REK.sendTheCmd("?action=volume_up", globalTV, all_TvType, btnNumber, false);
}
else if (globalRoom.equals("DSR")) {
btnNumber = 4;
DSR.sendTheCmd("?action=volume_up", globalTV, all_TvType, btnNumber, false);
}
else if (globalRoom.equals("LR2")) {
btnNumber = 0;
LR2.sendTheCmd("?action=volume_up", globalTV, all_TvType, btnNumber, false);
}
} else if (btnName.equals("down"))
if (globalRoom.equals("ABC")) {
btnNumber = 0;
ABC.sendTheCmd("?action=volume_down", globalTV, all_TvType, btnNumber, false);
}
else if (globalRoom.equals("NBC")) {
btnNumber = 1;
NBC.sendTheCmd("?action=volume_down", globalTV, all_TvType, btnNumber, false);
}
else if (globalRoom.equals("CBS")) {
btnNumber = 2;
CBS.sendTheCmd("?action=volume_down", globalTV, all_TvType, btnNumber, false);
}
else if (globalRoom.equals("REK")) {
btnNumber = 3;
REK.sendTheCmd("?action=volume_down", globalTV, all_TvType, btnNumber, false);
}
else if (globalRoom.equals("DSR")) {
btnNumber = 4;
DSR.sendTheCmd("?action=volume_down", globalTV, all_TvType, btnNumber, false);
}
else if (globalRoom.equals("LR2")) {
btnNumber = 0;
LR2.sendTheCmd("?action=volume_down", globalTV, all_TvType, btnNumber, false);
}
} etc etc....
In each of my classes ABC.java class:
public class ABC {
public static void sendTheCmd(String theCmd, String TV, List<ThemedButton> all_TvType, btnNumber, Boolean keyboard) {
try {
...
} catch (Exception e) {
Log.d("", e.getMessage());
}
return null;
}
}
NBC.java class:
public class NBC {
public static void sendTheCmd(String theCmd, String TV, List<ThemedButton> all_TvType, btnNumber, Boolean keyboard) {
try {
...
} catch (Exception e) {
Log.d("", e.getMessage());
}
return null;
}
}
So instead of having all those if/else I would like to make a variable that holds any of those classes (ABC, NBC, CBS, etc...) so that I can do something like this instead:
GlobalClass GC;
if (globalRoom.equals("ABC")) { btnNumber = 0; GC = new ABC(); }
else if (globalRoom.equals("NBC")) { btnNumber = 1; GC = new NBC(); }
else if (globalRoom.equals("CBS")) { btnNumber = 2; GC = new CBS(); }
else if (globalRoom.equals("REK")) { btnNumber = 3; GC = new REK(); }
else if (globalRoom.equals("DSR")) { btnNumber = 4; GC = new DSR(); }
else if (globalRoom.equals("LR2")) { btnNumber = 0; GC = new LR2(); }
if (btnName.equals("up")) {
GC.sendTheCmd("?action=volume_up", globalTV, all_TvType, btnNumber, false);
} else if (btnName.equals("down")) {
GC.sendTheCmd("?action=volume_down", globalTV, all_TvType, btnNumber, false);
} else if (btnName.equals("Keyboard")) {
GC.sendTheCmd("text?", globalTV, all_TvType, btnNumber, false);
} else if (btnName.equals("Other")) {
//Not sure what to make this icon
}
I was thinking I could do something like this:
List<String, Class<?>> GC;
Then populate it like this:
GC.put("ABC", new ABC());
GC.put("NBC", new NBC());
GC.put("CBS", new CBS());
GC.put("REK", new REK());
GC.put("DSR", new DSR());
GC.put("LR2", new LR2());
But that does not seem to work as I was expecting it to.
Is there any way of doing this?