This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ChoiceGroup getSelectedIndex() returns -1

 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a form that contains a Choicegroup with multiple choicetype. The form implemetns Command & ItemState Listeners.
When an choice is changed, itemStateChanged event is fired, but getSelectedIndex of the choicegroup return -1 only. I can't make out y? Where am I going wrong???



The output is Index = -1. The form returns to midlet on completion of its work; that all is handled & wprking perfectly. Just stuck over here.

Any idea why it is behaving like this.

Thanks
 
Darryl Burke
Bartender
Posts: 5149
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just another cross poster.
http://forums.sun.com/thread.jspa?threadID=5358429

Trupti, I've removed the solution I had posted last night on the Sun forum. I don't offer to help cross posters.
 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello Darryl,

Darryl, its not required that we will get help in 1 forum where we have posted. We might get differnt types of idea for the solution also. Do you intend to say that we should post on 1 forum, wait for N # of days, if didn't receive any satisfactory reply, then post in other forum. Wont that be wasting of N+ number of days for a solution. A sincere developer posts only when he/she needs some help, somethimes the help needed also might be urgent. I just don't post in forums unnecessarily - only when required. And when I get the solution, I always makrk it "Answered"/Close it. Now, it is upto you to help the needy or not.

Regards,

Trupti
 
Tim Holloway
Saloon Keeper
Posts: 18362
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be nice, people! That's what the JavaRanch is all about.

We don't mind if you cross-post on other sites. Not all of us visit all the different sites. I rarely hit the Sun one myself, because there's a lot of traffic there and I can't keep up and still maintain what little productivity I do have. Besides, unlike the JavaRanch, they have been known to flame on occasion, and I don't like flaming.

Cross-posting within the JavaRanch is quite another thing. We've found that it creates clutter and confusion, so we try and make sure that we have a set of forums designed so that for any particular question, there's usually one where you're most likely to find a lot of people who can give good answers.

But please don't depend on us if you need urgent help. We're all volunteers here, so we sign on and off at our convenience and if nobody has a good answer, the question may not get answered at all. When you need that level of support, there are places available elsewhere, but of course, they expect you to pay for it.
 
Suresh Chandra Pal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Trupti,

It is returning -1 as it should be. If you will see docs for ChoiceGroup, you will find that for ChoiceGroup.MULTIPLE, method getSelectedIndex() will always return -1 as there may be more than one options selected.

Instead you can use getSelectedFlags() method of ChoiceGroup. It requires a boolean type array. After calling this method you can loop through that array and can figure out which indexes are selected (Since the value at that index in array will be true).

My opinion is that this will resolve your problem
 
Trupti Mehta
Ranch Hand
Posts: 79
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh Chandra Pal wrote:Hello Trupti,

It is returning -1 as it should be. If you will see docs for ChoiceGroup, you will find that for ChoiceGroup.MULTIPLE, method getSelectedIndex() will always return -1 as there may be more than one options selected.

Instead you can use getSelectedFlags() method of ChoiceGroup. It requires a boolean type array. After calling this method you can loop through that array and can figure out which indexes are selected (Since the value at that index in array will be true).

My opinion is that this will resolve your problem


Thanks Suresh, your point abt ChoiceGroup.MULTIPLE is correct. I missed it from API.
but this doesn't solve my problem as I wanted.
My requiremtns are on selecting/deselecting any checkbox, I have to perform some calculation and display them on spot. So I want to trap which index is selected, do calculations and display in itemStateChange().

With getSelectedFlags, I get the results as you mentioned, but I got to check each time if any new item is selected/deselected. If so, search among the array and do accordingly. This increases the code and also affects performance. I don't think their is any alternative method to solve the query.

I also came to know that I got to have a text box also i.e.
Checkbox Text Text TextField

Was wondering how can I add textfield in the same line as the checkbox. Is their any other way that I can achive both checkbox & textfield in a same line and get access to items selected/deselected. Question on the same is asked at http://www.coderanch.com/t/425487/Java-Micro-Edition/Can-have-Checkbox-Textfield-together

If you can just help with the checkbox selections that would also be good.

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic