Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    How can I get the index of the selected value from SelectItem ?

    I have derived SelectItem to create a class of CollectionItem. My CollectionItem can contain a list of any serializable objects (integer, Double, String , ...). So in the CollectionItem , i can have multiple values which are identical.
    Let's say my CollectionItem has the following Integer values : 1 3 9 1
    If the user selects the last "1", and press delete button, my CollectionItem will delete the first "1". This is why I asked the question.

    Thank you for your replies.

    #2
    You have to use different keys to your value map.

    Comment


      #3
      Originally posted by jmichelgarcia
      You have to use different keys to your value map.
      Can you please give me an example?

      Comment


        #4
        You will need to provide some more info on how you are setting up the SelectItem. The key values must be unique, even if the displayed value is not.

        Comment


          #5
          As you can see from the code below, I set the values via setValueMap. I do not use key values and display values. To get the values selected, I use
          Code:
          collectionItem.getValues()
          Code:
          public class CollectionItem extends SelectItem {
          
          	private CollectionField field;
          	private ArrayList<Serializable> valueList;
          	private ArrayList<String> valueStringList;
          
          	public CollectionItem(CollectionField field) {
          		super(field.getText(), field.getText());
          		this.field = field;
          	}
          
          	private void generateLayout() {
          		setHeight(90);
          		setMultiple(true);
          		setMultipleAppearance(MultipleAppearance.GRID);
          		setWidth(200);
          	}
          
          	public CollectionField getField() {
          		return field;
          	}
          
          	public ArrayList<? extends Serializable> getValueList() {
          		return valueList;
          	}
          
          	public void addValue(Serializable value) {
          		if (valueList == null) {
          			valueList = new ArrayList<Serializable>();
          			valueStringList = new ArrayList<String>();
          		}
          		valueList.add(value);
          		valueStringList.add(value + "");
          		setValueMap(valueStringList.toArray(new String[0]));
          	}
          
          	public void removeValues(Serializable[] selectedValues) {
          		for (Serializable record : selectedValues) {
          			int index = valueStringList.indexOf(record + "");
          			valueList.remove(index);
          			valueStringList.remove(index);
          		}
          		clearValue();
          		setValueMap(valueStringList.toArray(new String[0]));
          	}
          
          	@Override
          	public Object getValue() {
          		return valueList;
          	}
          
          	@SuppressWarnings("unchecked")
          	@Override
          	public void setValue(Object value) {
          		clearValue();
          		valueList = (ArrayList<Serializable>) value;
          		if (valueStringList == null) {
          			valueStringList = new ArrayList<String>();
          		}
          		valueStringList.clear();
          		for (Serializable serialisable : valueList) {
          			valueStringList.add(serialisable + "");
          		}
          		setValueMap(valueStringList.toArray(new String[0]));
          	}
          
          }

          Comment


            #6
            Iin the end you are setting a plain array of String as the map. You can't do that when you have duplicate values. You need to use the LinkedHashMap option instead. Create the map using the index as the key, and the value as the display value. Then the value you get back from the SelectItem will be the index into the original list.

            Comment


              #7
              Originally posted by ssieb
              Iin the end you are setting a plain array of String as the map. You can't do that when you have duplicate values. You need to use the LinkedHashMap option instead. Create the map using the index as the key, and the value as the display value. Then the value you get back from the SelectItem will be the index into the original list.
              Thank you.

              Comment

              Working...
              X