SQLContainer iteration


How do I iterate over a SQLContainer, for example, to add all the data in one column to a string…




SQLContainer implements the IndexedContainer interface, so you can do something like this:

for (int i = 0; i < container.size(); i++) {
    Object id = container.getIdByIndex(i);
    Item item = container.getItem(id);
    // do stuff with item

The only thing to remember is to avoid the getItemIds() method if at all possible, since this method will load all rows from the database, which can be very slow if you have a huge dataset.

Or if you mean to display additional computed cells in a table, you should check out Table.addGeneratedColumn().


Thank you very much Jonatan, I was actually using getItemIds(), so you help a lot with your answer, which worked perfect…




I have a different problem now, and I’m not sure if I this is the right thread, but is that the getIdByIndex(i) method doesn’t work for hierarchical containers…

I’m try this now with a treetable (tree):

			for (int i = 0; i < tree.getContainerDataSource().size(); i++) {

				Item item = tree.getContainerDataSource().getItem(tree.getContainerDataSource().getItemIds());
	            Object value =item.getItemProperty(PROPERTY).getValue();


…but it gives me a null pointer exception error…I’m not sure how to fix this…any idea??

regards and ,as always, thank you very much…


Sorry for the late reply, but I don’t really understand the code you posted. You talk about getIdByIndex(), but don’t call it anywhere. Instead you have a call to getItem(getItemIds()), which I’m sure will return NULL, since getItemIds() will return a collection and not an ID for any item.

Anyway, I guess you already solved the problem :slight_smile: