java - Substitute for if statements when using Constants -


i think best way describe question, describe doing first.

i have simple activity 3 listpick options (buttons launch listview). each listpicker has 8 items in listview.

each item in listview, let's call value of name, has corresponding address , phone.

so here example of constatns 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 item gets picked listpicker, , want evaluate result

i have 3 strings need set; name, address, , phone

so right have 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 on , forth.

so, here question. there easier way allows me evaluate if selection equals 1 of name constants , if so, set corresponding values?

i think need improve code in 2 following directions:

create new simple class aggregates name, phone , 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 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 can access contacts looking map:

contact contact = selections.get(selection); 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -