Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Set not adding object when selecting items from Listbox

Tags:

java

zk

jooq

I am using Java, Jooq and Zk7 for my application but i am getting one issue see my scenario I have Listbox which showing all the records in model i Have a add button when click on it i am adding new element in Listbox something like this

    private ListModelList<Record> adminListModelListSort;
    protected ListModelList<Record> adminListModelList;

    ValidWorkRecord new_record1 =  new ValidWorkRecord();
    adminListModelList.add(new_record1);
    adminNewListModelList.add(new_record1);

Listbox adding item no any issue but when i am taking back selected item i am getting single record And select item is something like this

WorkCode|WorkDesc|StatCanWorkType|StampDate|StampUser|AccessCode|WorkDesc2|Comments|ActiveFlag|
+--------+--------+---------------+---------+---------+----------+---------+--------+----------+
| *{null}|*{null} |*{null}        |*{null}  |*{null}  |   *{null}|*{null}  |*{null} |*{null}   |

So i added 5 new items in the Listbox and its showing 5 item added but when i am trying to get selecteditem it show only 1 item added if i am making any change in newly added item then it showing that item as a selected . Note:-first time newly added item content null values only . Do you know what is the issue with java Set

private Set  selectedListItem;//get,set method

But when i tried below code everything worked fine ZUL Page

    <?page title="new page title" contentType="text/html;charset=UTF-8"?>
<zk>
 <window title="new page title" border="normal" id="inp"
  viewModel="@id('vm') @init('test.MyListbox')" apply="org.zkoss.bind.BindComposer"  >
  <button label="AddItem" onClick="@command('addNewItem')"  ></button>


  <listbox model="@bind(vm.dataList)" selectedItems="@bind(vm.selectitems)" multiple="true" checkmark="true">
   <listhead>
    <listheader value="A"></listheader>
    <listheader value="B"></listheader>
    <listheader value="C"></listheader>

   </listhead>
   <template name="model" var="mymodel">
    <listitem>
     <listcell>

      <textbox value="@bind(mymodel.a)" />
     </listcell>
     <listcell>
      <label value="@bind(mymodel.b)" />

     </listcell>
     <listcell>
      <label value="@bind(mymodel.c)" />

     </listcell>
    </listitem>
   </template>
  </listbox>
 </window>
</zk>

View Model Code

package test;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;

public class MyListbox {

 private List<Data> dataList;
 private Set selectitems;

 @AfterCompose
 public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
  try {
   dataList = new ArrayList<Data>();
   Data data;
   data = new Data("a1", "b1", "c1");
   dataList.add(data);
   data = new Data("a2", "b2", "c2");
   dataList.add(data);
   data = new Data("a3", "b3", "c3");
   dataList.add(data);
  } catch (Exception e) {

  }
 }



 public Set getSelectitems() {
    return selectitems;
}



public void setSelectitems(Set selectitems) {
    this.selectitems = selectitems;
}



@Command
 @NotifyChange("dataList")
 public void addNewItem(){
  Data data = new Data(null, null, null);
  dataList.add(data);
  if(selectitems != null)
  System.out.println(selectitems.size());
 }
 public List<Data> getDataList() {
  return dataList;
 }

 public void setDataList(List<Data> dataList) {
  this.dataList = dataList;
 }

 public class Data {
  String a;
  String b;
  String c;
  public String getA() {
   return a;
  }
  public String getB() {
   return b;
  }
  public String getC() {
   return c;
  }
  public void setA(String a) {
   this.a = a;
  }
  public void setB(String b) {
   this.b = b;
  }
  public void setC(String c) {
   this.c = c;
  }
  public Data(String a, String b, String c) {
   super();
   this.a = a;
   this.b = b;
   this.c = c;
  }

 }
}

Why Set behaving two way in two places?

like image 891
JavaBeigner Avatar asked Apr 15 '14 07:04

JavaBeigner


1 Answers

A Set cannot contain duplicates, but a List can. 2 objects o1 and o2 are considered duplicates if o1.equals(o2) returns true (or both are null). If you try to add a duplicate, it will be ignored (add() will return false).

If you always add your items this way:

ValidWorkRecord new_record1 =  new ValidWorkRecord();
selectedListItem.add(new_record1);

then there is a chance that they are considered equal, depending on your equals() method.

Indeed, calling the constructor without other modifications will initialize all your items the same way. If you overrode equals() so that it is not based on the pointer value anymore, then you should check the return value of add(...) to be sure your items are added.


UPDATE: You don't seem to control the add(...) statement as far as I understand, so you can't check for its return value. But, now that I see your data class, you haven't overriden equals() so this shouldn't be the problem.

However, you probably shouldn't use the raw type Set, but Set<Data> instead.

Also, try and put a trace in your setter setSelectitems() to check if the method is at least called.

like image 89
Joffrey Avatar answered Sep 20 '22 02:09

Joffrey