Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 30
Registered: ‎02-25-2009
My Device: Not Specified

ListField.getSelectedIndex returning invalid values

I'm getting different values for ListField.getSelectedIndex depending where I call it from. If I call list.getSelectedIndex() in a MenuItem's run method it ALWAYS returns zero no matter what the actual value of the list's selected item is. Outside of the MenuItem's run method I get the correct value. I checked the id of the list object and they are the same in both of the places I am checking the return value of getSelectedIndex. Poking around with the debugger the Listfield's _cursor value is always zero in the MenuItems run method, but the _cursor memebr is correct outside the MenuItem's run method.

 

What am I doing wrong?

 

I'm testing this using eclipse with the 4.3.0 component and simulator.

 

Developer
Posts: 507
Registered: ‎07-15-2008
My Device: Not Specified

Re: ListField.getSelectedIndex returning invalid values

Do you have other fields than your ListField instance on the screen ?

 

Maybe there is another field focused when you are calling getSelectedIndex()

----------------
if your issue solved set "Solution" mark at the relevant post
Developer
Posts: 30
Registered: ‎02-25-2009
My Device: Not Specified

Re: ListField.getSelectedIndex returning invalid values

Should it matter? Why would it be correct in one place and incorrect in another?

 

I would guess the menu that I'm choosing from is showing. The list is showing behind the menu.

 

I have a timer running and it is System.out.printlning the result of list.getSelectedIndex. This print out is always correct. Menu showing or not. But the MenuItem's run method gets 0 back from the lists getSelectedIndex.

 

I'm stumped..

 

 

 

Developer
Posts: 507
Registered: ‎07-15-2008
My Device: Not Specified

Re: ListField.getSelectedIndex returning invalid values

I will clarify my question: how many ListField instances do you have on the screen?

----------------
if your issue solved set "Solution" mark at the relevant post
Developer
Posts: 30
Registered: ‎02-25-2009
My Device: Not Specified

Re: ListField.getSelectedIndex returning invalid values

I'm sorry, I misunderstood. Just one. But now that you ask and I think a bit, could it be that I'm getting the menu's selected item instead of the list's selected item even though I'm calling list.getSelectedItem?

 

It would make sense. I'm choosing the first menu item in the list which is index zero.

 

But then why on earth would calling list.getSelectedIndex return the selected item of the menu?

 

Am I going to have to track the selected item of the list myself?

 

Developer
Posts: 507
Registered: ‎07-15-2008
My Device: Not Specified

Re: ListField.getSelectedIndex returning invalid values

very strange issue.

If you have one list field instance, let's say:

ListField myListField = new ListField();

have ListFieldCallback instance specified for this list field, let's say:

myListField.setCallback(myListFieldCallbackInstance);

and are calling: myListField.getSelectedIndex()

it must return the index of current selected item in the list.

If it does not work - make a very simple app with listfield inside and check this out step by step.
----------------
if your issue solved set "Solution" mark at the relevant post
Developer
Posts: 30
Registered: ‎02-25-2009
My Device: Not Specified

Re: ListField.getSelectedIndex returning invalid values

I worked around this by adding an int member variable named currentlySelectedItem with a getter method to my ListField subclass. I then modified the moveFocus override in the subclass like this:

 

public int moveFocus(int amount, int status, int time) {

  currentlySelectedItem = getSelectedIndex(); 

  invalidate(currentlySelectedItem);

  return super.moveFocus(amount, status, time);

}

 

since getSelectedIndex returns the correct value here. I then call the getter method for currentlySelectedItem in the MenuItem run method rather than getSelectedIndex.

 

Seems to work for me... 

Highlighted
Contributor
Posts: 14
Registered: ‎06-07-2010
My Device: 8100
My Carrier: Simulator

Re: ListField.getSelectedIndex returning invalid values

[ Edited ]

Hi,

Check if there is any where else you are updating the ListField.

 

 

I was having the same problem because I was updating the values in ListField  on the onExposed Method of the Screen.

 

This way my ListField was getting reset and it was returning zero

Arjav Dave
Trusted Contributor
Posts: 108
Registered: ‎01-18-2012
My Device: 9360
My Carrier: AIRTEL

Re: ListField.getSelectedIndex returning invalid values

arja.. ur spot on... but how did you resolve the refresh ? I need to refresh my page from some other screens..

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: ListField.getSelectedIndex returning invalid values