X

Shay Shmeltzer's Oracle Development Tools Tips

  • November 3, 2009

Getting the Value from a SelectOneChoice List Using Code

Shay Shmeltzer
Director of Product Management - Oracle

I got asked this today, and this seems to be a question that pops up every now and again, so I thought I'll document this little piece of code.

The scenario is that you have a drop down list using a selectOneChoice component on your page - and you want to find out the selected value in a backing bean.

For example the departmentId field in this page:

The first thing everyone try and do is to access the list component in a backing bean and invoke the getValue method on it. But then they realize that this only returns the actual index of the row that was selected and not the value.

The following little piece of code will get you that value.

What it does is access the bindings object, get the list binding, and then invoke the getSelectedValue method on it.

public void buttonPressed(ActionEvent actionEvent) {

// Get the binding

BindingContainer bindings =

BindingContext.getCurrent().getCurrentBindingsEntry();

// Get the sepecific list binding

JUCtrlListBinding listBinding =

(JUCtrlListBinding)bindings.get("DepartmentId");

// Get the value which is currently selected

Object selectedValue = listBinding.getSelectedValue();

System.out.println(selectedValue);

}


Note that the binding name I'm getting (DepartmentId) is the last one in this picture - see the list binding icon next to it.

lov002.gif

Join the discussion

Comments ( 13 )
  • Maiko Rocha Friday, November 6, 2009
    The other trick that I use all the time is to create AttributeBindings based on the same iterator used for the listBinding. This way instead of coding I can just use EL to grab the current value. The only difference in this case is that you need to set the selectOneChoice to autoSubmit so the attributeBinding gets updated with the selected value. This is a good trick if you need to show a description write to the side of your selectOneChoice, or anywhere else in the page.
    []s! :-)
  • Jayaram Friday, June 18, 2010
    Thanks Shay, this was indeed what i was looking for too.
  • guest Thursday, October 6, 2011

    Hi Shay,

    I used your code in the AM method and it complied fine.

    But when I run it, it gives the below error

    oracle.adfinternal.view.faces.model.binding.FacesCtrlAttrsBinding cannot be cast to oracle.jbo.uicli.binding.JUCtrlListBinding

    Why I am putting it in AM method? Because based on this value I am executing another view.

    Thanks for your help.


  • Shay Thursday, October 6, 2011

    Guest - you are doing something wrong if you are using faces/view layer classes in your AM. The AM is suppose to be independent from any specific View/client technology.

    The right approach is to use AM service method, see:

    http://blogs.oracle.com/shay/entry/am_service_method_-_simple_dem

    Then you can even bind the parameter to an LOV, see:

    http://blogs.oracle.com/shay/2011/01/adding_an_lov_to_a_query_param.html


  • guest Thursday, September 27, 2012

    Hi Shay,

    I was googling around when I bump into your post.

    In this particular post of yours, you are getting the value from the select one choice.

    I have this question, what if you want to set the value of the selectonechoice which is bound in the bindings? Is there a utility method that will set the value of the JUCtrlListBinding

    A sample use case is that, for example in other parts of the UI, you selected something and you need to update the DepartmentId to a particular value from the list then how can you do this?

    Thanks

    Marky


  • Achinto Friday, December 21, 2012

    HI Shay,

    Very well put together.


  • guest Friday, December 28, 2012

    I am using backing bean to populate the list instead of data binding and when I am trying to get the value, getting index. Pls let me know how to get the actual value in this case.


  • guest Tuesday, January 8, 2013

    Hi Shay,

    I am able to resolve the issue as by reading the label information of that particular select item rather than value. Reading value of the select item gives numeric number where as reading label gives the description.

    Thanks a lot for providing the valuable information's.


  • guest Tuesday, August 27, 2013

    This won't work if you define the LOV in the VO. It doesn't show up in the bindings like you show.

    Also, I'm not sure it will work if your selectonechoice is in an AF:Table.


  • guest Tuesday, August 27, 2013

    Ok, I think I finally got this worked out.

    If you're in a validator, you don't have access to the "value change" stuff.

    So, I did this:

    MyConfViewRowImpl vConfStat = (MyConfViewRowImpl)JSFUtils.resolveExpression("#{row.bindings.CnfrRqstStatCd.valueList[" + pObject + "]}");

    String vNewCdString = vConfStat.getCnfrRqstStatCd();


  • guest Monday, November 3, 2014

    When I am trying to get value from Drop Down by using this logic following value I get every time what is mean it?

    ViewRow [oracle.jbo.Key[]]


  • guest Tuesday, April 28, 2015

    I also get the same issue. ViewRow [oracle.jbo.Key[300000860721156 ]] i am getting. how to find the exact value.. The value is 300000860721156. Its a long type. Would you please help me.


  • David Gress Thursday, October 5, 2017
    I'm getting this ViewRow [oracle.jbo.Key[]] as well. I'm using JDev 12.2.1.1.0.
    Has something changed in later version of JDev?
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.