addItem into container

I have a form with some fields. When I execute the insert statement within the db I must insert a new item in the container too and set visible immediately in my table. The problem is that when I execute this action I get always this exception:

“Adding items not supported. Override the relevant addItem*() methods if required as specified in AbstractInMemoryContainer javadoc.”

I tried to override the addItem() method in different ways but nothing change. Why?

I have my entity with its properties:

Activity act = new Activity();

act.setPropA(ect…ect);
act.setPropB(ect…ect);

and so on…

then I would to execute the addItem from my form with this method:

table.addItem();

I passed the itemId of course, but I can’t to solve this exception and I understood that I don’t know how this method must be implemented.

Help me please. :frowning:

I just checked the code and from that I would say, that it should work … Can you provide the relevant code-snippets? Which Container-implementation are you using?

I’m using BeanContainer.

Here is the code:


public void addActivity(){
		
		key 			= new ActivityKey();
		act 			= new Activity();
		key.setCode_society(selectSociety.getValue().toString());
		key.setCod_orde(selectOrder.getValue().toString());
		key.setCod_activity(activityField.getValue().toString());	
		
		act.setId(key);
		act.setDescr_activity(this.getField("desc-act").getValue().toString());
		if(staatt.getValue().equals(true))
			att.setState_activity("A");
		else
			att.setState_activity("I");
		
                table.addItem(act.getId());

		table.activityDao.save(act);	//		
		
		getWindow().showNotification(app.getBundle().getString("insert-ok"),Notification.TYPE_HUMANIZED_MESSAGE);
		
		
		
		setReadOnly(true);
	}

In my tableClass I do this:



public TableActivity extends Table {
        public TableActivity (ResourceBundle bundle) {
                setSizeFull();  
		
		setContainerDataSource(loadActivity());	
		
		setColumnHeader("id.cod_society",bundle.getString("cod-soc"));		
		setColumnHeader("id.cod_order",bundle.getString("cod-com"));
		setColumnHeader("id.cod_activity",bundle.getString("cod-att"));
		setColumnHeader("descr_activity",bundle.getString("desc-att"));
		setColumnHeader("state_activity",bundle.getString("sta-att"));
		
		// set the order of the columns as well as in the entity
		setVisibleColumns(new Object[] { "id.cod_society","id.cod_order","id.cod_activity","descr_activity","state_activity"});
}

private Container loadActivity() throws ClassNotFoundException, SQLException{
		
		ArrayList<Activity> act = new ArrayList<Activity>();
		act= (ArrayList<Activity>) activityDao.getAllActivity();			
		BeanContainer<String, Activity> bean = new BeanContainer<String, Activity>(Activity.class);			
		bean.addNestedContainerProperty("id.cod_society");
		bean.addNestedContainerProperty("id.cod_order");
		bean.addNestedContainerProperty("id.cod_activity");
		bean.setBeanIdProperty("id");
		for(int i=0;i<act.size();i++){							
			bean.addBean(act.get(i));
		}
		return bean;  	
	}
}

I don’t know how to implements the addItem() method…

:frowning:

Use either addBean() - the Item-id will be computed using a BeanItemIdResolver you have to specify - or addItem(Object id, Object bean).

I understood what to do, but I don’t know how to do it…Sorry, but something of vaadin are not so simply for me!! :frowning:

Remember that the first piece of code that I posted above is in the class that extends the form, while the second part is in the class that extends table…I must addItem in the container from form

In your form:

((BeanContainer) table.getContainerDataSource()).addBean(act);

or

((BeanContainer) table.getContainerDataSource()).addItem(act.getId(), act);

I’m very silly…I tried so many times to write this istruction but without results because I didn’t do the cast in BeanContainer… I am ashamed of the simplicity of the solution.

Thank you Tobias. In my office nobody knows vaadin and I’m studing and working with it without help and somethings are unknown for me yet. Thanks :slight_smile: