9
I’m developing an application web, where I have a list of an object called frames
, well, every time I click on a button I add a new object to that list, only the problem is this: When I try to add a new object for the second time to that list, the first object that was inserted previously disappears, as if nothing was inserted, leaving only the 2nd inserted object and not the 1st and 2nd, ie, is always guarding the last inserted object. How do I save as many objects as needed to add to this list?
Well I have it so far, when the user click add, it will open a screen to fill in some information about which object I mentioned: This is the button:
<p:commandLink id="btn_close_users_modal3" actionListener="#{messageBean.insertFrame()}"
styleClass="btn btn-default" >
<i class="fa fa-plus fa-fw" /> #{bundle['system.ui.label.add.frameAdd']}
</p:commandLink>
And that’s my method, where what you have to do and then add to a list.
public void insertFrame() {
try {
// caso nao visualize a mensagem e salve direto, chama de qualquer forma esse metodo
teste();
frame.setContent(svg);
frame.setWriteContent(objSvg.getValueText1() + " / " + objSvg.getValueText2());
frame.setRemoved("f");
frame.setOrder(1);
frame.setLogo('f');
listAllFrames.add(frame);
MessageGrowl.info(MessageProperties.getString("message.sucesso"));
RequestContext.getCurrentInstance().execute("PF('framesModal').hide();");
// frameFacade.save(frame);
} catch (Exception e) {
e.printStackTrace();
}
}
Ivan, try to make available what you have already done so far, this ensures greater attention to your question, if possible edit your question with a minimum example to reproduce the question.
– Wellington Avelino
"List" is an ambiguous term in a Java context. Is that a List, or an Arraylist, or something else? Post code and highlight or simulate the specific problem, otherwise it is difficult to give a concrete answer.
– Ericson Willians
I edited the question, I think it was clearer.
– Ivan
Your
listAllFrames
is instantiated as?– Maicon Carraro
listAllFrames = new Arraylist<>(); first I was instantiating, then I withdrew this method there
– Ivan
Ivan, if you were instantiating within the method every call of the method is a new Arraylist created, and dispensed with when the method returns. The correct thing is that you install it as class property. You better post all Class code, or at least more details relevant to this process.
– Delfino
Related Why can’t I access the elements inside an Arraylist with a for?
– Sorack