Android Question Spinner question

anaylor01

Well-Known Member
Licensed User
Longtime User
How do you set the value for different values. Say you select the third value how can you change the text value for it.?
 

anaylor01

Well-Known Member
Licensed User
Longtime User
Well now it runs all the way through but the spinner doesn't reload with values. The spinner is blank.
B4X:
   Dim anInputDialog As InputDialog
   anInputDialog.Input=Value
   anInputDialog.Show("New Value:","Edit Pos" & Position & " - " & Value,"OK","Cancel","",Null)
   If anInputDialog.Response=DialogResponse.POSITIVE Then
     Dim newValue As String=anInputDialog.Input
aList.Initialize
aList.InsertAt(Position,newValue)
aList.RemoveAt(Position)
spnNameTeams.Clear
spnNameTeams.AddAll(aList)
Msgbox(aList,"alist")
   End If
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
aList.InsertAt(Position,newValue)
aList.RemoveAt(Position)
So, you are Adding a value at position 0 for ex.
And then you are removing item 0

You are wondering this will not change anything?

Switch the order of your two commands

BUT First; REMOVE The initialization of the list like you got told a few times now
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Well now it runs all the way through but the spinner doesn't reload with values. The spinner is blank.
End If[/CODE]

Because you are initializing the list just before you insert 1 item .. then remove 1 item . The list is Empty!

B4X:
Dim anInputDialog AsInputDialog
 anInputDialog.Input=Value
 anInputDialog.Show("New Value:","Edit Pos" & Position & " - " & Value,"OK","Cancel","",Null)If anInputDialog.Response=DialogResponse.POSITIVE ThenDim newValue AsString=anInputDialog.Input

    ' aList.Initialize   @@@@@@@  Initialize and populate the list elsewhere ... another sub 

aList.InsertAt(Position,newValue)
aList.RemoveAt(Position)
spnNameTeams.Clear
spnNameTeams.AddAll(aList)Msgbox(aList,"alist")EndIf
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
@DonManfred Swapping the order in this example will only error , as the list is initialized immediately prior . The list is empty

Edit ... The list should be initialized and populated elsewhere ... or am i missing something ?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Edit ... The list should be initialized and populated elsewhere ... or am i missing something ?
No. You are absolutely right!
I did not realized that he already has the init there. You already told him to remove it.
But away from that the order should be changed to get a change

I for myself give up on this guy and this topic
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
You are doing the same mistake.
Adding a item at pos 0 (TEST)
and then removing item 0 (TEST)
will result in the same list as before

But the main reason this example is wrong because the list is initialized just before adding item

for clarity I re post copy of @mc73's code ... this is the correct way !
B4X:
Sub aSpinner_ItemClick (Position As Int, Value As Object)Dim anInputDialog AsInputDialog
anInputDialog.Input=Value
anInputDialog.Show("New Value:","Edit Pos" & Position & " - " & Value,"OK","Cancel","",Null)If anInputDialog.Response=DialogResponse.POSITIVE ThenDim newValue AsString=anInputDialog.Input
aList.InsertAt(Position,newValue)
aList.RemoveAt(Position+1)
aSpinner.Clear
aSpinner.AddAll(aList)EndIf
End Sub]

@anaylor01 .. The list must be initialized elsewhere
 
Last edited:
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
re-edit your post. your answer and the quote got mixed.
But you are right. @mc73 code is right
I need more coffee it seems. Sorry
No sorry's needed .. :) it should not have come this far for a solution !
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Any suggestions where I should initialize it? I tried the button click that make the spinner visible. But still get a need to initialize error.
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Any suggestions where I should initialize it? I tried the button click that make the spinner visible. But still get a need to initialize error.
In Activity Create / Resume or Starter service if you have one ..
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Then shouldn't in the button_click process work? That is what I tried and I still get the java.lang.RuntimeException: Object should first be initialized (List).
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Then shouldn't in the button_click process work? That is what I tried and I still get the java.lang.RuntimeException: Object should first be initialized (List).

Yes .. You can initialize the list in a button_click event As long as there is NO reference to the list BEFORE the button is clicked.
But that is why you are getting this error.

Everytime you initialize the list you have to re-add all spinner items to the list first .. why not do it only once.
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
This is only an example initializing the list in a button_click event. It is not the way I would do it. ... it reuses code by @mc73

B4X:
Sub Activity_Create(FirstTime As Boolean)
  Activity.LoadLayout("main")  'Contains only button
  aSpinner.Initialize("aSpinner")
  Activity.AddView(aSpinner,0,0,50%x,15%y)
  aSpinner.AddAll(Array As String("item 1","item 2","item 3"))
  aSpinner.Visible = False
End Sub

Sub aSpinner_ItemClick (Position As Int, Value As Object)
  Dim anInputDialog As InputDialog
  anInputDialog.Input=Value
  anInputDialog.Show("New Value:","Edit Pos" & Position & " - " & Value,"OK","Cancel","",Null)
  If anInputDialog.Response=DialogResponse.POSITIVE Then
  Dim newValue As String=anInputDialog.Input
  aList.InsertAt(Position,newValue)
  aList.RemoveAt(Position+1)
  aSpinner.Clear
  aSpinner.AddAll(aList)
    Msgbox(aList,"alist")
  End If
End Sub

Sub Button1_Click   
   aList.Initialize 
   For i = 0 To aSpinner.Size -1     
     aList.Add(aSpinner.GetItem(i))
   Next
   aSpinner.Visible = True
End Sub
 
Upvote 0

anaylor01

Well-Known Member
Licensed User
Longtime User
Maybe you can tell me how I should do it. What happens is the user clicks on the spinner and an input box opens. The user enters a value. I then want that value added to the spinner.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe you can tell me how I should do it.
Maybe you should start learning the language after you got helpful answers

Parts of the solution you can find in this thread. Also you got shown the correct way to add an dynamic entry to a specific position in your global list of spinner-entries.

All this normall should allow you to find the solution by yourself.
All it needs; you need to understand what you need to do (work with the spinner items; and how) and you need to understand how to work with (global) lists.

In fact; all this is described in the Beginners guide and the Users guide. MAybe go back a step and start there again
 
Last edited:
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Maybe you can tell me how I should do it. What happens is the user clicks on the spinner and an input box opens. The user enters a value. I then want that value added to the spinner.

With all the code snippets offered in this thread , You should be able to put together a working project that allows user to input NewTeam and Edit existing one.
If you cannot you should follow @DonManfred's suggestion to read the available Guides.

Also it may help if you were to re - read this thread again and study all that has been offered ... ;)

B4X:
Sub Globals
   Private aSpinner As Spinner
   Private edtTeams As EditText
   Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
  aSpinner.Initialize("aSpinner")
  Activity.AddView(aSpinner,50dip,50dip,50%x,15%y)

  edtTeams.Initialize("edtTeams")
  edtTeams.TextSize = 24
  edtTeams.Hint = "Add New Team"
  edtTeams.SingleLine = True
  edtTeams.ForceDoneButton = True
  edtTeams.Visible = False
  Activity.AddView(edtTeams, 50dip,40%y , 20%x, 60dip)

  Button1.Initialize("Button1")
  Button1.Text = "Add New Team"
  Activity.AddView(Button1, 50dip, 60%y, 20%x, 60dip)
End Sub

Sub Button1_Click
  edtTeams.Visible = True
End Sub

Sub edtTeams_EnterPressed
   aSpinner.Add(edtTeams.Text)
   edtTeams.Text = ""
   edtTeams.Visible = False
End Sub
 
Upvote 0
Top