1

Im trying to create a two lists in vf page from the key and values of a map, if i select a key from one list,then the other list should populate the values of that key..Below is the apex class and VF page for the same. Im facing error in this code,could you please help me put..please suggest if we have any other sotions also.Please excuse for bad code indenting... Apex Class

public class mapdepend {
public string mycity {get;set;}
map<string,List<string>> mybranches= new map<string,List<string>>();
public list<selectoption> city{get;set;}
public list<selectoption> branch{get;set;}
  public mapdepend(){
    list<string> hyd = new list<string>();
    hyd.add('SR Nagar');
    hyd.add('LB Nagar');
    List<string>  bang=  new list<string>();
    bang.add('Ecity');
    bang.add('Matahalli');
    mybranches.put('hyd',hyd);
    mybranches.put('bang',bang);
    set<string> keys= mybranches.keyset();
    city=new list<selectoption>();
    branch=new list<selectoption>();
    List<string> my = new list<string>();
    my.add('None');
     for(string a: keys)
      {
        selectoption op1= new selectoption(a,a);
        city.add(op1);
      }
      }
   public pagereference show() 
       {
       for (mycity : mybranches.keyset())
           { 
           branch.add(mybranches.get(mycity));
           }
       return null;
       }
    }

VF page:

<apex:page controller="mapdepend">
<apex:form >
 <apex:selectlist value="{!mycity}" size="1"> 
 <apex:selectoptions value="{!city}">
 </apex:selectoptions>
 <apex:actionsupport event="onchange" action="{!show}" rerender="one"/>
 </apex:selectlist>
 <apex:selectList size="1"  id="one">
 <apex:selectOptions value="{!branch}"></apex:selectOptions>
 </apex:selectList>
</apex:form> 
</apex:page>

2 Answers 2

1

In this line

for (mycity : mybranches.keyset())

you are not providing the data type of mycity variable. So change that line to

for (String mycity : mybranches.keyset())

EDIT:

Although the suggested solution may solve your current error, you will run into some other errors.

branch is list<SelectOption> so you cannot do branch.add(mybranches.get(mycity)); because you're trying to add list<String> to list<SelectOption>.

Replace

branch.add(mybranches.get(mycity));

with

for(String branchString : mybranches.get(mycity)){
    branch.add(new SelectOption(branchString,branchString);
}
3
  • @Vignesh- I have given datatype and executed the code.. Incompatible element type List<String> for collection of System.SelectOption at line 30 column 12 error occured
    – Yusuf Syed
    Commented Mar 16, 2016 at 18:39
  • Take a look at my updated code. Commented Mar 16, 2016 at 18:42
  • Logic im looking for: I need to get the list of values associated for a single key in map and store the same list in another list(list<selectoption>)
    – Yusuf Syed
    Commented Mar 16, 2016 at 18:44
2

You aren't declaring what you are iterating through.

Just above the error, you are using another for loop where you are declaring what to iterate through:

for(string a: keys)
  {
    selectoption op1= new selectoption(a,a);
    city.add(op1);
  }

Here you are stating, for every item in Keys create a string variable called a and do the followin code.

for (mycity : mybranches.keyset())
   { 
   branch.add(mybranches.get(mycity));
   }

Try declaring the mycity variable as the same type of information in your mybranches.keyset():

for (string mycity : mybranches.keyset())
   { 
   branch.add(mybranches.get(mycity));
   }
1
  • 1
    @Zainogj-Thanks for pointing at variable declaration
    – Yusuf Syed
    Commented Mar 16, 2016 at 18:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .