0

Retreive data(model) from listItem

asked 2010-02-10 10:25:19 +0800

LuigiX gravatar image LuigiX
12

hey there,
I'm very new to ZK so forgive me if I could seem dumb.
Btw I'm just trying to figure out how get my data back by (selected) listItems into a listBox.

I have this method in my Controller class, in-code comments explains my problem

public void onSelect$listBox() {

		
		// with this I can get all selected items from my multicolumn Listbox (rapresenting a portion of my POJO)
                // but I'm able to retrive just the first column by using the getLabel() method.

                Set set = listBox.getSelectedItems();
		for (java.util.Iterator<Listitem> it = set.iterator(); it.hasNext();) {
			Listitem li = it.next();
			System.out.println(li.getLabel());
		}



                // With this portion of code I can get all MyPojos but I don't know if they are selected or not.
                // Of course I could use the indexes I've retreived just few lines of code ago, but I don't like this
                // dirty solution.

		ListModelList lml = (ListModelList) listBox.getModel();
		for (java.util.Iterator it =lml.iterator(); it.hasNext();){
                        //got mypojos!!! YES!   
			MyPojo mp = (MyPojo )it.next();
		}	
}


Any idea?
Thanks!

Luigi

delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2010-02-10 12:27:11 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2010-02-10 12:28:20 +0800

You can CAST to your pojo


      Set set = listBox.getSelectedItems();
		for (java.util.Iterator<Listitem> it = set.iterator(); it.hasNext();) {
			Listitem li = it.next();

                        //got mypojos!!! YES!   
			MyPojo mp = (MyPojo )it;

			System.out.println(li.getLabel());
		}

link publish delete flag offensive edit

answered 2010-02-11 02:52:18 +0800

LuigiX gravatar image LuigiX
12

oh yep, I forgot to say I already tried that ;)
Of course it didn't work as I get a sad ClassCastException

java.lang.ClassCastException: org.zkoss.zul.Listitem cannot be cast to it.prototype.datalayer.MyPojo

link publish delete flag offensive edit

answered 2010-02-11 04:47:05 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2010-02-11 04:48:14 +0800

OK.
Have a look on the followed sources of an Listbox ItemRenderer.
The pojo class is called 'Branch'.
See how to put the pojo in the ListItem and get them back.

/**
 * Item renderer for listitems in the listbox.
 * 
 * @author bbruhns
 * @author sgerth
 * 
 */
public class BranchListModelItemRenderer implements ListitemRenderer,
		Serializable {

	private static final long serialVersionUID = 1L;

	@Override
	public void render(Listitem item, Object data) throws Exception {

		Branche branche = (Branche) data;

		Listcell lc = new Listcell(branche.getBraBezeichnung());
		lc.setParent(item);

		item.setAttribute("data", data);

		// ComponentsCtrl.applyForward(item, "onClick=onClicked");
		ComponentsCtrl.applyForward(item, "onDoubleClick=onDoubleClicked");
	}
}

	/**
	 * Call the Branch dialog with the selected entry. <br>
	 * <br>
	 * This methode is forwarded from the listboxes item renderer. <br>
	 * see: de.forsthaus.webui.branch.model.BranchListModelItemRenderer.java <br>
	 * 
	 * @param event
	 * @throws Exception
	 */
	public void onDoubleClicked(Event event) throws Exception {

		// get the selected object
		Listitem item = listBoxBranch.getSelectedItem();

		if (item != null) {
			// CAST TO THE SELECTED OBJECT
			Branche aBranche = (Branche) item.getAttribute("data");

			if (logger.isDebugEnabled()) {
				logger.debug("--> " + aBranche.getBraBezeichnung());
			}

			showDetailView(aBranche);
		}
	}

best
Stephan

link publish delete flag offensive edit

answered 2011-05-09 09:10:15 +0800

airton gravatar image airton
78 1

I've done so:

index.zul:

<window id="win" apply="MyController">
...
		
<listbox id="myList" model="@{win$composer.myPojo}" checkmark="true" multiple="true">
   <listhead>
      <listheader label="Code" width="100px" />
      <listheader label="Name" />
   </listhead>
   <listitem self="@{each='objects'}" value="@{objects}"> 
      <listcell label="@{objects.code}"/>
      <listcell label="@{objects.name}"/>
   </listitem>
</listbox>

<button id="buttonSelection" label="Selection"></button>


MyController.java:

public List<MyPojo> getMyPojo() {
... getting the list of objects to populate myList...
}


Listbox myList;

public void onClick$buttonSelection() {
   Set<Listitem> select = myList.getSelectedItems();

   for (Listitem li : select){
      code = ((MyPojo)li.getValue()).getCode();
      name = ((MyPojo)li.getValue()).getName();
      anythingElseOnMyPojo = ((MyPojo)li.getValue()).getAnythingElseOnMyPojo(); 
      ...
   }

}

link publish delete flag offensive edit

answered 2011-05-09 14:54:25 +0800

caclark gravatar image caclark
1753 2 5
http://clarktrips.intltwi...

This thread is 15 months old. Are you asking a question or just giving an example of how you did it? I don't see how yours is going to work because of the signature of your onClick$buttonSelection() method. It should be onClick$buttonSelection(Event evt)...

link publish delete flag offensive edit

answered 2011-05-09 17:48:21 +0800

airton gravatar image airton
78 1

Hi, Cary.

I'm just giving an example.

I've done it this way and is working, including the method signature.

I was looking for a solution to "Listitem cannot be cast..." when I've found this old thread. I've thought if I put my solution here it could help someone...

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2010-02-10 10:25:19 +0800

Seen: 786 times

Last updated: May 09 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More