Android Question Changing Label Value with number not working

Discussion in 'Android Questions' started by aaronk, Apr 6, 2015.

  1. aaronk

    aaronk Well-Known Member Licensed User

    Hi,

    I can't work out why this is not working and hoping someone can work it out..

    First of all I am loading a layout which has 3 labels and 6 buttons in it.

    What I am trying to do is adjust the label value as you press on the buttons.
    When the Activity loads the default values are:
    Hours = 0
    Mins = 0
    pmam = "AM"

    I am using the following code to adjust the values..

    Code:
    Sub hour_up_Click
        
    If hours.Text = 23 Then Return
        hours.Text = hours.Text + 
    1
    End Sub
    Sub hour_down_Click
        
    If hours.Text = 1 Then Return
        hours.Text = hours.Text - 
    1
    End Sub
    Sub mins_up_Click
        
    If mins.Text = 59 Then Return
        mins.Text = mins.Text + 
    1
    End Sub
    Sub mins_down_Click
        
    If mins.Text = 0 Then Return
        mins.Text = mins.Text - 
    1
    End Sub
    Sub ampm_up_Click
        
    If pmam.Text = "PM" Then Return
        pmam.Text = 
    "PM"
        hours.Text = hours.Text + 
    12
    End Sub
    Sub ampm_down_Click
        
    If pmam.Text = "AM" Then Return
        pmam.Text = 
    "AM"
        hours.Text = hours.Text - 
    12
    End Sub
    Problem I have is when I press the buttons to adjust the value it's changing it to 0.1, 0.2 etc.

    (hours, mins, pmam are all labels that are created with the designer script and loaded to the Activity from the Activity_Create Sub)

    Any ideas on what I am doing wrong ?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Can you upload your project (File - Export as zip)?
     
  3. aaronk

    aaronk Well-Known Member Licensed User

    See the attached file..

    You will notice when you tap on the + and - buttons it should count up like 1, 2, 3 etc. however it will count like 0.1, 0.2, 0.3 etc.
     

    Attached Files:

  4. aaronk

    aaronk Well-Known Member Licensed User

    I think I might of worked it out..

    In my Sub Globals I put:
    Code:
    Dim hour_value As Int = 0
    Dim min_value As Int = 0
    Then I changed the value using this:
    Code:
    Sub hour_up_Click
        
    If hours.Text = 23 Then Return
        hour_value = hour_value + 
    1
        hours.Text = hour_value
    End Sub
    Sub hour_down_Click
        
    If hours.Text = 1 Then Return
        hour_value = hour_value - 
    1
        hours.Text = hour_value
    End Sub
    Sub mins_up_Click
        
    If mins.Text = 59 Then Return
        min_value = min_value + 
    1
        mins.Text = min_value
    End Sub
    Sub mins_down_Click
        
    If mins.Text = 0 Then Return
        min_value = min_value - 
    1
        mins.Text = min_value
    End Sub
    Sub ampm_up_Click
        
    If pmam.Text = "PM" Then Return
        pmam.Text = 
    "PM"
        hours.Text = hours.Text + 
    12
    End Sub
    Sub ampm_down_Click
        
    If pmam.Text = "AM" Then Return
        pmam.Text = 
    "AM"
        hours.Text = hours.Text - 
    12
    End Sub
    Now it works like it should, however still don't know why the other way isn't working.
     
  5. Mahares

    Mahares Well Known Member Licensed User

    I downloaded and tested your project on a Galaxy tab2 with OS 4.2.2 and it works like it is supposed to, without any issues. The numbers increase or decrease by one. It is odd you were getting the behavior you were.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    You should change your code to use NumberFormat:
    Code:
    mins.Text = NumberFormat(mins.Text + 100)
     
  7. aaronk

    aaronk Well-Known Member Licensed User

    I am using my Nexus 5 running Android Lollipop, so maybe it doesn't work correctly with Android Lollipop.

    This seems to fix it, thanks heaps.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice