Reputation: 827
I am so sorry for this simple question (I have only been working with java for a little bit).
I have an Array List of check boxes and they have a corresponding label with it from another Array List.
i want to be able to set a string variable to a certain value depending on what the label of the selected check box is. I need to compare up to four selected check boxes. So if check box A, with label A = X, then the serial number of that device = Y.
I have tried to use the contains() method, but it returns null. I have tried to use get(index).getText(), but that only works for a single index (when I have up to four that I need to evaluate).
Any ideas and suggestions are welcome.
Thanks!!
ironmantis7x
===================================================================
Here is a part of my code with the suggestion below:
if (PlatformPanel.Android.isSelected() == true)
{
Iterator<JCheckBox> listIter = Devices.selectedDevices.iterator();
while(listIter.hasNext())
{
JCheckBox nextItemInList = listIter.next();
if (nextItemInList.toString().equals("HTC Droid Eris"))
//if (Devices.selectedDevices.iterator().toString() .equals("HTC Droid Eris"))
{
selectedSerial = "A100000DA78159";
System.out.println(selectedSerial);
System.out.println(Devices.selectedDevices.get(0));
}
if (nextItemInList.toString().equals("Asus Transformer Prime (#1)"))
//if (Devices.selectedDevices.iterator().toString() .equals("Asus Transformer Prime (#1)"))
{
selectedSerial = "BKOKAS127271";
System.out.println(selectedSerial);
System.out.println(Devices.selectedDevices.get(0));
}
}
}
Upvotes: 1
Views: 205
Reputation: 425318
I would use a static map (static
means it's a class field, rather than an instance field, so all instances get to re-use the one map):
public class MyClass {
private static Map<String, String> serialNumbers = new HashMap<String, String>() {{
put("HTC Droid Eris", "A100000DA78159");
put("Asus Transformer Prime (#1)", "BKOKAS127271");
// etc
}};
// rest of class
}
then in your code, it's a one-liner:
String selectedValue = ...; // get it directly from the drop down
String selectedSerial = serialNumbers.get(selectedValue);
Upvotes: 1
Reputation: 66657
You may Iterate over the arraylist and compare with String.
Example: Assuming your list is of type String
.
Iterator<String> listIter = yourList.iterator();
while(listIter.hasNext())
{
String nextItemInList = listIter.next();
if(nextItemInList.equals("YourOtherString")
{
//Do your logic.
}
}
Upvotes: 0