thebenCA
thebenCA

Reputation: 159

Increment Int onClick

Noob Android developer here.

I'm creating a basic American Football scorekeeping app for Android. I am trying to increment a variable value with the clock of a XML button.

For some reason, the variable isn't updated as desired when increment button is clicked. I'm sure I'm missing something basic but can't seem to crack it.

<TextView
        android:id="@+id/home_touchdowns_var"
        android:layout_width="10dp"
        android:layout_height="24dp"
        android:layout_marginStart="80dp"
        android:layout_marginTop="16dp"
        android:text="0"
        android:textSize="16sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/home_touchdowns" />

 <Button
        android:id="@+id/home_touchdown_plus"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="homeTouchdownButtonPlus"
        android:text="+"
        app:layout_constraintStart_toEndOf="@+id/home_touchdowns_var"
        app:layout_constraintTop_toBottomOf="@+id/home_touchdowns" />



 package com.example.android.scorekeeper;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Display;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    int homeScore = 0;
    int homeTouchdowns = 0;
    int homeXtraPoints = 0;
    int homeFieldGoals = 0;

    int visitorScore = 0;
    int visitorTouchdowns = 0;
    int visitorXtraPoints = 0;
    int visitorFieldGoals = 0;


    public void homeTouchdownButtonPlus(View view) {
        homeTouchdowns = homeTouchdowns + 1;
    }

    public void homeTouchdownsButtonMinus(View view) {
        homeTouchdowns = homeTouchdowns - 1;
    }
}

Upvotes: 0

Views: 963

Answers (2)

[UPDATED]

Your code is right, but is not completed, you have to call the method setText of the TextView to update the view data, and also you have to map the TextView in your code, so your code will be:

private TextView textview_home_touchdowns; //TextView where you will see the data

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textview_home_touchdowns = (TextView) //Mapping the TextView
                                   findViewById(R.id.home_touchdowns_var);
}

public void homeTouchdownButtonPlus(View view) {
    homeTouchdowns = homeTouchdowns + 1;
    textview_home_touchdowns.setText(homeTouchdowns+""); //Updating the TextView as String

}

public void homeTouchdownsButtonMinus(View view) {
    homeTouchdowns = homeTouchdowns - 1;
    textview_home_touchdowns.setText(homeTouchdowns+"");
}

Thank you @Martin De Simone for the correction

Upvotes: 1

Martin De Simone
Martin De Simone

Reputation: 2148

@DigaoParceiro is right, you forgot to call the setText(), but if you do it the way he did it it will throw an exception, cause the setText method expects a String not an integer, use this.

public void homeTouchdownButtonPlus(View view) {
    homeTouchdowns = homeTouchdowns + 1;
    textview_home_touchdowns.setText(String.valueOf(homeTouchdowns)); //Updating the View
}

public void homeTouchdownsButtonMinus(View view) {
    homeTouchdowns = homeTouchdowns - 1;
    textview_home_touchdowns.setText(String.valueOf(homeTouchdowns));
}

Upvotes: 1

Related Questions