Menu

#12 List - But I need some advice

open
nobody
None
5
2007-09-19
2007-09-19
tieTYT
No

I'd like to add a multi select list question type to jsurveylib, but I need some advice before I do it. The problem with a list is that it's the first question where the setAnswer(String)/String getAnswer doesn't make sense with a String.

Here are three ways to do this, which all have their pros and cons:

1) Every item in the list is conceptually its own question. For example, imagine a list with 3 items in it: one, two and three. If one and three are selected, the answers look like this:
one: selected
two: unselected
three: selected

And you set it to this state setting one to selected, two to unselected and three to selected in three steps. To get the state, you'd have to do 3 separate getAnswer()'s

2) The list and all of its items are considered a single question. Now we must solve the problem of being able to interpret the selection of the whole list in ONE string. The only way I can think of doing this is by using some special character that signifies a separation between items that are selected. Using the example above, we'd represent the answers like this (assuming * is the delimiter):
one*three

And to set it this way, you'd call setAnswer("one*three"); all in one step. To get the answer, you'd call getAnswer() and have to interpret the results by parsing the String. This shouldn't be too difficult, I think a simple String#contains will work fine to figure out if the item is selected.

As I type this, the latter seems "obviously" better, so I better explain what I consider the cons to this. As a general rule, I don't like putting non-xml data in xml that has to be parsed. In fact, there's even a dailywtf article about how stupid this is to do: http://worsethanfailure.com/Articles/XML_vs_CSV__0x3a__The_Choice_is_Obvious.aspx

3) One solution to this is that I could set/get the answer by embedding XML right into the answers. For example:
<selected>one</selected><unselected>two</unselected><selected>three</selected>

You'd have to get/set this by using the exact string above (maybe the unselected doesn't need to be there). This options, frankly, just seems weird to me. I haven't really thought out all the pros and cons to this, but my first reaction is how difficult it is to work with for anything other than XML. For example, imagine trying to figure out if an id called "select" was selected. This becomes a much more complicated task than String#contains("select") because this word is a substring of <selected>.

Ok so I need some advice. What do you think?

Discussion

  • tieTYT

    tieTYT - 2007-09-19

    Logged In: YES
    user_id=1313948
    Originator: YES

    > This shouldn't be too difficult, I think a simple String#contains will work fine to figure out if the item is selected.

    I realize the fault in this logic. It's possible for one id in the list to be a substring of another id. This would give false results. The solution to this is to search by String#contains("*idWeAreLookingFor*") and either put *'s on both sides of the answer or also check String#beginsWith("idWeAreLookingFor*") and a similar idea with endsWith("*idWeAreLookingFor")

     
  • Nobody/Anonymous

    Logged In: NO

    You can keep the single question-structure for checkboxes, but include another grouping tag for checkboxes.
    F.e. a multiselectquestion-tag.

    <multiselectquestion id="multiid">
    <label >Which color?</label>
    <question id="Red">
    <label>Red</label>
    <checkbox/>
    </question>

    <question id="Green">
    <label>Green</label>
    <checkbox/>
    </question>

    <question id="Yellow">
    <label>Yellow</label>
    <checkbox/>
    </question>
    </multiselectquestion>

    With this approach you keep the question-answer-value concept, which can be mapped easily to hashmaps, csv-files...

    The group tag shall be identified in beanshell scripts too, so you can toggle their status (visible, enable, validate).

    What do you think? Is it possible in this way?

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.