Android Question Setting Spinner Selections by Index is not reliable?

Todd Carlton

Member
Licensed User
Longtime User
Hello Community,

I am writing an app where one of my spinners has hundreds of items in it alphabetically. Instead of making a user scroll though hundreds of entries, I have created 26 buttons (A-Z) that should allow them to 'jump' to the right area of the spinner to find their choice.

The problem is, I cannot always get the spinner to 'move to' the correct area every time.

Attached is a very simple example of how I move the spinner around and how it is not cooperating.

---

EXPERIMENT #1:

Run the app for the first time. Spinner defaults to "A".

Without touching the spinner, click Button1 (M).

Tap the spinner. Drop down selections start at "M" item (correct).
Tap the spinner to close it. (No not make a selection.)

Now click button 2 (D).
Spinner shows "D".
Tap the spinner. Drop down selections still start at "M" item (wrong).

Tap the spinner. Select "P".
Tap the spinner. Drop down starts at "P" item (correct).
Tap the spinner to close it. (No not make a selection.)

Now click button 2 (D).
Spinner shows "D".
Tap the spinner. Drop down still starts at the "P" item (wrong).

Tap the spinner. Select "O"
Tap the spinner. Drop down shows area of the "O" item (correct).
Tap the spinner to close it. (No not make a selection.)

Now click button 1 (M).
Spinner shows "M".
Tap the spinner. Drop down shows area of the "O" item (wrong).

---

Experiment #2:

Run the app for the first time. Spinner defaults to "A".

Tap the spinner to view the drop down selections. Selections start at "A" item (correct).
Tap spinner again to close it without making a selection.

Click button 1 (M).
Spinner shows "M".
Tap the spinner. Drop down selections still start at "A" item (wrong).

(This just worked fine in Experiment #1; although this time peeking at the spinner first prevented it from moving.)

---

I cannot get this app to crash in the same way, but in the real app, after doing the above routine multiple times, another routine (not in this project) crashes because of the index. If I always scroll through the spinner instead of moving it around by index, the app never crashes.

What is a better / the right way to move to spinner around to select items that may be hundreds of items away from the present selection?
 

Attachments

  • Spinner_Challenge.zip
    7.4 KB · Views: 234

Erel

B4X founder
Staff member
Licensed User
Longtime User
There were several problems in your code.

The correct code is:
B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim sf As StringFunctions
   sf.initialize   

   Dim Spinner1 As Spinner
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Main")
   Dim AB As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
   Dim ct As Int = 0
   For ct = 0 To AB.Length - 1
     Spinner1.Add(AB.CharAt(ct))
   Next
   Spinner1.SelectedIndex=0
End Sub
The index is correct. This issue cannot cause your app to crash. It only doesn't update the dropdown position.

This is how the native Spinner behaves. It only updates the position when the dropdown items are created.
 
Upvote 0

Todd Carlton

Member
Licensed User
Longtime User
Thank you Erel for your quick reply.

So, if we disregard the way I load the spinner for the example and forget my mention of later crashing...

When I set a Spinner index by not 'peeking' at the spinner first and the dropdown updates properly, and I then I 'peek' at the spinner and then set the index and the dropdown position doesn't update... that's the normal Spinner behavior? It doesn't populate the dropdown options from the index value?

Based upon that, I don't see why it even works in Experiment 1 then. All that Experiment 1 changes is the index value and the position is updated.

Experiments simplified...

"EXPERIMENT #1:
Run the app for the first time. Spinner defaults to "A".
Without touching the spinner, click Button1 (M).
Tap the spinner. Drop down position selections start at "M" item (correct)."

"EXPERIMENT #2:
Run the app for the first time. Spinner defaults to "A".
Tap the spinner to view the drop down selections. Selections start at "A" item (correct).
Tap spinner again to close it without making a selection.
Click Button1 (M)
Tap the spinner. Drop down selections start at "A" item (wrong)."

Why would previewing the position stop the spinner from updating the position when the index changes when without previewing it works fine? Spinner preview breaking position update sounds like a flaw to me. Please review and confirm.

Or, is there an instruction to manually update spinner position besides setting the index?
 
Upvote 0

Todd Carlton

Member
Licensed User
Longtime User
The Spinner is a native view. I agree that it is strange that it is not updating the scroller position and I did try several methods to update the position. However they didn't work.

How many items are in your spinner?

Erel... Thank you for taking a second look. 1,029 items in the spinner currently. Based upon data acquisition for the application (the spinner is part of a database look-up tool) that will grow at a rate of about 120-150 items per year.

Martin... I am currently using Reflection to 'open' the spinner after an alpha-key is pressed in order to encourage the end user to select 'something' which will allow the spinner position to update properly for the next alpha-key selection.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Look at this post of mine: http://www.b4x.com/android/forum/threads/spinner-change-adapter.40533/#post-243179.

You can use a custom Adapter with your Spinner instead of using the Adapter that is the Spinner's default Adapter.
If you have time to try compiling the 2 examples in the above post you'll see switching from a default Adapter to a custom Adapter isn't hard work.
Some basic android xml layout file creation is required - the forum will help there if you lack this skill.

I'm thinking that either:
  • My custom Adapter library may not exhibit the same behaviour as the default Adapter - a bit of a long shot but it might worth a try.
  • If the problem persists then my BaseAdapter has a NotifyDataSetChanged method and calling this might fix the problem.

If you go with a custom Adapter you have the added bonus that you can fully customise the appearance of each Spinner item!

Martin.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Erel... Thank you for taking a second look. 1,029 items in the spinner currently. Based upon data acquisition for the application (the spinner is part of a database look-up tool) that will grow at a rate of about 120-150 items per year.
Measure how fast it takes to create the spinner and fill it with 1000 or 2000 items. My guess is that it will be fast enough. If this issue is significant for you then you can remove the current spinner and create a new one instead when you want to update the index programmatically.
 
Upvote 0
Top