Custom item for a list

jeffnooch

Member
Licensed User
Longtime User
What I want to do is create a list or spinner or something that is pre-populated with an array of values that a user can select from...i've done this with a list in the past...assuming i have the following items in an array 10, 20, 30 ...

1) is there a way i can set it up so that the user can enter a custom item easily? eg 70 (doesn't need to be stored back to original array) ... just looking for a way for user to select something not listed if possible and store to same variable as if i picked something from the list...

2) would i be best served with a list, spinner or something else?

Thanks
 

sorex

Expert
Licensed User
Longtime User
I guess you can just add a list item with text "other value" and as value=-1

in the click event you check for -1 and if that's the case you can bring up a textfield or one of those number selectors. when selected add it to the array and resort it on text value (if that's possible)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
How about something like this Jeff:
B4X:
'Globals
Dim MySpinner As Spinner  'Created in designer
Dim MyValue  As Int

'Activity_Create
MySpinner.AddAll(Array As Int(0,10,20,30,40,60,80,99)) 



Sub MySpinner_Itemclick(Position As Int, Value As Object)
   MyValue=Value
   If MyValue=99 Then MyValue=ManualInput
   Msgbox(100*MyValue,"result")
End Sub

Sub ManualInput As Int
   Dim MyNumber As NumberDialog  'need dialog lib
   Dim ret As Int
      ret=MyNumber.Show("Please enter a number not in list","ok","Cancel","",Null)
      If ret=-1 Then
         Return MyNumber.Number
      End If
End Sub
 
Upvote 0

jeffnooch

Member
Licensed User
Longtime User
Mahares,
I adapted what you have there and it works well enough for what i'm trying to do...thanks...appreciate your help...

one other question...is there any way in the spinner to not have the 0 (in your example) show by default...also is there a way to change the font in the spinner?

Thanks
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
You can make the list as string like I show below. The rest of the code is OK, except that you want to trap when the user selects the empty string in the first item so the calculation does not crash:
B4X:
MySpinner.TextSize=24  'line must be before adding items to list
MySpinner.TextColor=Colors.Red  'line must be before adding items
MySpinner.AddAll(Array As String("","10","20","30","40","60","80","99"))

You can change the text size and color as I show above. Not sure about the font.
 
Upvote 0

jeffnooch

Member
Licensed User
Longtime User
Mahares...thanks for the response...i tweaked things a bit and went with a label (since can customize font/layout/etc a bit more) and click event that brings up a list (rather than spinner) that then populates the label...
i appreciate your help...got me pointed in right direction...
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
How can you simulate a spinner or listview with a label, select a single item from the label that has a list of items stored in it and be able to assign a value to it. I am interested to know how you did it or can do it code wise.
Thank you for your creativity.
 
Last edited:
Upvote 0

jeffnooch

Member
Licensed User
Longtime User
what i did was the following (Not sure if this is best way...but it allowed me to set the typeface and other font features a little easier than the spinner):

added label named lblTest in design view and put something like "Click Me" in the text property

B4X:
Sub lblTest_Click
   Dim lstTest As List
   Dim result As Int
   lstTest.Initialize
   lstTest.AddAll(Array As String("10","20", "30", "Custom"))

   result = InputList(lstTest,"Pick from List",1)
   If result < 0 Then 
      lblTest.Text=lblTest.Text
   Else If lstTest.Get(result) = "Custom" Then
       myValue=ManualInput       'using variation of your ManualInput code from above
      lblTest.Text = myValue
   Else
      lblTest.Text = lstTest.Get(result)
   End If
End Sub

hope that helps...
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Thanks jeff. That makes sense. I thought the list is stored inside the label itself, in which case how would you select an item inside it. But, that is creative what you did. If it works, nothing else matters.
 
Upvote 0
Top