Search code examples
javadebuggingcompiler-errorsiteratorgxt

Iteration over a ListStore in Java Gxt


So I am working with grid widgets in java...and when trying to iterate over the ListStore I get the following error.

[javac]   required: array or java.lang.Iterable
[javac]   found:    ListStore<String>

Any tips for how I would solve this/ create an iterator for this?

Here is my code:

public void cycle(ListStore<String> line_data){

    for(LineObject line: line_data){
          //Other code goes here
    }


}

Solution

  • As the javadoc shows List Store does not implement Iterable. So you cannot iterate over it using a for each loop.

    Just use the getAll() method of List Store which returns you a java.util.List which properly implements Iterable.

    But an additional issue is that you are trying to iterate using LineObject which will not work since your ListStore is declared using String i.e. ListStore<String> and not ListStore<LineObject>

    Here is some sample code:

    public void cycle(ListStore<String> line_data){
    
        List<String> lineListData = line_data.getAll();
    
        //for(LineObject line: lineListData){ <-- won't work since you are using Strings
    
        for(String line: lineListData){ // <-- this will work but probably not what you want
              //Other code goes here
        }
    
    }
    

    Looking back on your edits to your question you probably just want to use LineObject:

    public void cycle(ListStore<LineObject> line_data){
    
        List<LineObject> lineListData = line_data.getAll();
    
        for(LineObject line: lineListData){
              //Other code goes here
        }
    
    }