-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi guys,
I got problem with selecting Listitem..
I'm have an Listbox with datas and checkmark="true",
i have an 5 row, every row i added attribute for the item id while in rendering :
public class EmployeesListModelItemRenderer implements ListitemRenderer, Serializable { public EmployeesListModelItemRenderer() { super(); } @Override public void render(Listitem item, Object data) throws Exception { listitem.setAttribute("itemid",uniqueStringId()); item.setAttribute("data", data); } }
in case..
i double click the row and code execute
Listitem item = lstbox_emp.getSelectedItem(); String itemid = item.getAttribute("itemid").toString(); . . . . . .
. . . . . listbox.setModel(...); listbox.setItemRenderer(...); listbox.setSelectedItem(getLastSelectedItem(listbox.getItems(), itemid)); . . . .
Method getLastSelectedItem():
11 Listitem getLastSelectedItem(List<Listitem> items, String itemid){ 12 for (org.zkoss.zul.Listitem listitem : items) { 13 if (listitem.getAttribute("itemid").equals(itemid)) 14 return listitem; 15 return new Listitem(); 16 }
Thx.
Hi mahiMahi,
only a hint for selecting a known Object as a ListItem in a Listbox:
myListBox.setModel(new ListModelList(getMyServiceDAO().getAList()));
myListBox.setItemRenderer(new myListModelItemRenderer());
// get the ListModelList back for this Listbox for work with it
ListModelList lml = (ListModelList) myListBox.getModel();
// get Object that we want to select in the Listbox
Branche branche = customer.getBranche();
// select the ListItem in the Listbox by an integerPosition by getting
// it's position in the corresponding ListModelList (lml)
myListBox.setSelectedIndex( lml.indexOf( branche ));
best
Stephan
Hi Sthepan,
That's work!!!.
Nice hint.
So just use lstbox.setSelectedIndex(int index)
by using lstmodel.indexOf(object) for the index
instead of lstbox.setSelectedItem(Listitem item)
by assign and checking attribute value through the item.
Thanks,
Fadhlan
Hi Sthepan,
I tried the example but the sentence lml.indexOf(lang) always returns -1
In debug mode I can see that the variable lang is within lml but still not found
What I'm doing wrong
This is the code
lbox_catIdCatalogo.setModel(new ListModelList(getAvanteGTHService().getItemsCatalogo(Sistema.CATALOGO_MASTER,0)));
lbox_catIdCatalogo.setItemRenderer(new CatalogosDropDownListModelItemRenderer());
ListModelList lml = (ListModelList) lbox_catIdCatalogo.getModel();
AvCatalogos lang = getAvanteGTHService().getItemByCatalogoAndIdDato(Sistema.CATALOGO_MASTER, itemCatalogo.getVidcatalogo(),0 );
int ff = lml.indexOf(lang);
lbox_catIdCatalogo.setSelectedIndex(lml.indexOf(lang));
Is there another way to Select Listitem in Listbox
Thanks
first i think you need to check the list from "getAvanteGTHService().getItemsCatalogo(Sistema.CATALOGO_MASTER,0)" contains the lang ?
if there exist , you may check this below
for java docs explain the behavior in list ,
http://download.oracle.com/javase/1.5.0/docs/api/java/util/List.html#indexOf(java.lang.Object)
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#equals(java.lang.Object)
it is talking about List.indexOf will call object.equals to check if the object is equals ,
and the equals method inherits from Object is default to compare with instance.
I guess the problem is , this one instance in list is not same as lang ,
so if you dont overwirte the equals , it will think that is not same object in list. so you got a message for -1 ( item not found )
if you can modifly the AvCatalogos class , you should try to overwrite equals method to make sure it will work correctly.
or just try the simple way , to iterate over collections and compare each object by yourself.
i think there may be some field about "id" or something you can use.
just like
lml.indexOf(lang) int index = 0 ; for(int i=0;i<lml.size();++i){ AvCatalogos item = (AvCatalogos ) lml.get(i); if(item.getId() == lang.getId()){ index = il } } // dosomething with index
It's exactly like TonyQ said.
Look at your bean class and let Eclipse generate the hashCode and equals method for you.
Thanks Terrytornado and TonyQ
Now work,
I add this code in Avcatalogos class
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + iiditemcatalogo;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
AvCatalogos other = (AvCatalogos) obj;
if (iiditemcatalogo != other.iiditemcatalogo)
return false;
return true;
}
Asked: 2009-12-30 01:04:34 +0800
Seen: 1,659 times
Last updated: Aug 20 '10