简体   繁体   中英

List of List of Buttons is empty

I'm working around with GWT.

Since I have a SQL db and my queries result set size is unknown I thought it would make sense to use Lists.

Actually I have one list of buttons and a second list of list of buttons.

The reason is because I have one table which stores groups and one which stores the actual data. Both of them in result should be buttons.

By clicking on a group button my layout is filled with the data buttons between the group buttons.

Now my db connection isn't ready to use so I wrote a function that fills my lists with fake data. Same for the groups.

public void fakeGroupData () {
// Group index 0
    btnGroupList.add(new Button("Group a"));
// Group index 1
    btnGroupList.add(new Button("Group b"));
    ...
}

public void fakeData () {

// Group index 0
    btnDataList.add(new Button("Data 1.1"));
    btnDataList.add(new Button("Data 1.2"));
    btnDataList.add(new Button("Data 1.3"));
    btnDataListList.add(btnDataList);
    btnDataList.clear();

// Group index 1
    btnDataList.add(...
}

Declaration looks like this

List<Button> btnGroupList = new ArrayList<Button>();
List<List<Button>> btnDataListList = new ArrayList<List<Button>>();
List<Button> btnDataList = new ArrayList<Button>();

When trying to get the ButtonList of the ListList the error appears.

    int grpIndex = Panel.getWidgetIndex(grpBtn);
// grpBtn is equal to (Button)event.getSource() called by btnGroup ClickHandler
    btnDataList.clear()
    btnDataList = btnDataListList.get(grpIndex);
    int loopEnd = btnDataList.size() - 1;

    for (int i = 0; i<=loopEnd; i++) {...

"loopEnd" contains "-1" and nothing happens :(. I tried to debug here, everything seems ok. "grpIndex" has the right index so the the right List is loaded. But why is it empty? When debuggin the fakeData function eclipse shows the right size in the ButtonList.

Hope you can help me :)

btnDataList.clear(); will empty your referenced list objects.

for each Group index you need a new list. do like this

btnDataList = new ArrayList<Button>(); instead of btnDataList.clear();

// Group index 0
btnDataList.add(new Button("Data 1.1"));
btnDataList.add(new Button("Data 1.2"));
btnDataListList.add(btnDataList);

// Group index 1
btnDataList = new ArrayList<Button>();
btnDataList.add(new Button("Data 2.1"));
btnDataList.add(new Button("Data 2.2"));
btnDataListList.add(btnDataList);

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