简体   繁体   中英

GWT ComboBox Chronological Order

I have the GWT ComboBox widget(ListBox+TextBox) .User can enter some text into this widget or select from the list of previously existing texts by clicking on the dropdown arrow.

So ,i have an ArrayList and when ever a user enters text and clicks the "Search Button" i perform a search if the entered text exists in the ArrayList and if it doesnt exist i add the text to the ArrayList .

Then later when i need to display the ComboBox in the View, i am iterating through my ArrayList and adding every String in the Arraylist to ComboBox.

The way the values are displayed in the Combo Box are not in Chronological order.The latest entered text is at the bottom of the ComboBox.How can i make this Chronological?

I can reverse the list and add it to my ComboBox.But when a user selects a text which is already existing thats when the problem is.(I can sublist the ArrayList ,push the selected item to the top and make adjustments to the sublist and add it back to the main ArrayList,but i feel its too long and not effective)

So are there any suggestions with respect to the data structure that i am using(ArrayList)? which could make my task easier?

Sorry for being over elaborate but i always feel its better to be than not to.

Does this sample code manage the list of criteria in the way you wanted:

import java.util.*;
public class ListManager {

public static void main(String[] args) {
    String[] items = {"snow", "rain", "ice", "sleet", "ice", "sunny"};
    List<String> criteriaList = new ArrayList<String>();
    String criteria;
    for (String newCriteria : items) {
        if (criteriaList.remove(newCriteria)) {
            System.out.println(newCriteria + " was in the list already");
        }
        criteriaList.add(0, newCriteria);
        System.out.println();
        System.out.print("List is: ");
        for (String x : criteriaList) {
            System.out.print(x +"  ");
        }
        System.out.println();
    }
}

}

The output is:

List is: snow

List is: rain snow

List is: ice rain snow

List is: sleet ice rain snow

ice was in the list already

List is: ice sleet rain snow

List is: sunny ice sleet rain snow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM