Reputation: 881
I think the best way to describe my question, is to describe what I am doing first.
I have a simple activity with 3 listpick options (Buttons that launch a listview). Each ListPicker has 8 items in the ListView.
Each Item in the Listview, let's call it a value of Name, has a corresponding Address and Phone.
So here are the example of the constatns I am using
public final String GROUP_1_VENUE_1_NAME = "1name1";
public final String GROUP_1_VENUE_1_ADDRESS = "1address1";
public final String GROUP_1_VENUE_1_PHONE = "1phone1";
public final String GROUP_1_VENUE_2_NAME = "1name2";
public final String GROUP_1_VENUE_2_ADDRESS = "1address2";
public final String GROUP_1_VENUE_2_PHONE = "1phone2";
public final String GROUP_1_VENUE_3_NAME = "1name3";
public final String GROUP_1_VENUE_3_ADDRESS = "1address3";
public final String GROUP_1_VENUE_3_PHONE = "1phone3";
.....
public final String GROUP_2_VENUE_1_NAME = "2name1";
public final String GROUP_2_VENUE_1_ADDRESS = "2address1";
public final String GROUP_2_VENUE_1_PHONE = "2phone1";
public final String GROUP_2_VENUE_2_NAME = "2name2";
public final String GROUP_2_VENUE_2_ADDRESS = "2address2";
public final String GROUP_2_VENUE_2_PHONE = "2phone2";
....
public final String GROUP_3_VENUE_1_NAME = "3name1";
public final String GROUP_3_VENUE_1_ADDRESS = "3address1";
public final String GROUP_3_VENUE_1_PHONE = "3phone1";
public final String GROUP_3_VENUE_2_NAME = "3name2";
public final String GROUP_3_VENUE_2_ADDRESS = "3address2";
public final String GROUP_3_VENUE_2_PHONE = "3phone2";
...
So an item gets picked from a listPicker, and now I want to evaluate the result
I have 3 Strings that need to be set; name, address, and phone
so right now I have something like this...
if (selection.equals(GROUP_1_VENUE_1_NAME) {
name = GROUP_1_VENUE_1_NAME;
address = GROUP_1_VENUE_1_ADDRESS;
phone = GROUP_1_VENUE_1_PHONE;
} else if (selection.equals(GROUP_1_VENUE_2_NAME) {
name = GROUP_1_VENUE_2_NAME;
address = GROUP_1_VENUE_2_ADDRESS;
phone = GROUP_1_VENUE_2_PHONE;
} else if .....
and so on and so forth.
So, here is my question. Is there any easier way to do this that allows me to evaluate if the selection equals one of the NAME CONSTANTS and if so, set the corresponding values?
Upvotes: 0
Views: 119
Reputation: 7692
One better OOP
way would be to encapsulated related fields sat in Person
class:
public class Person{
String name;
String address;
String phone;
}
and then use a Map<String, Person>
, fill it up with person objects and associated with specific name, then at time of selection you could simply have logic like:
Person selectedPerson = map.get(selection);
and use Person
attributes (name, address, phone) where you want.
Upvotes: 0
Reputation:
You could prepare a map each for addresses and phones of type Map<String, String>
Then you would just have to check, whether the name is known (or skip the part if the input is always known), and then fill in the values, i.e.:
// Preparation
addresses.put(GROUP_1_VENUE_1_NAME, GROUP_1_VENUE_1_ADDRESS);
phones.put(GROUP_1_VENUE_1_NAME, GROUP_1_VENUE_1_PHONE);
...
// Evaluation
name = selection;
address = addresses.get(name);
phone = phones.get(name);
Upvotes: 0
Reputation: 10161
I think you need to improve your code in two following directions:
Create new simple class that aggregates name, phone and address:
class Contact {
private final String name;
private final String phone;
private final String address;
public Contact(String name, String phone, String address) {
this.name = name;
this.phone = phone;
this.address = address;
}
// getters
}
Use Map to store selection mappings:
private static final Map<String, Contact> selections = new HashMap<String, Contact>();
static {
selections.put("3name1", new Contact("name1", "phone1", "address1"));
// other selections
}
Then you can access your contacts by looking up map:
Contact contact = selections.get(selection);
Upvotes: 2
Reputation: 40408
use an enum
for your constants. Then you can set it up nicely and switch on it too :)
Upvotes: 0