CK61938
CK61938

Reputation: 19

Same private methods to be used in one public method

Please see the below code. I am creating a score keeping app and I have it set up to record certain stats for each team. So when I hit the + button for one team I want it to update that stat and then the same thing for the other team without it updating for the 1st team. I have also included a screenshot of the app itself (do please forgive the colouring for now lol). Is anyone able to provide any assistance, please? Football Score Counter App

So basically: If I select the free kick option under the Home Team, I want it to update that and if the Away Team gets awarded a penalty, I want to be able to update that without updating for both teams at the same time. Or just update for one team. As that's what it's doing right now.

Android Studio tells me I cannot have "private void display(int number)" as it matches with the other one further down. So how do I differentiate them?

Many thanks and I hope I have done my best to explain it properly!

public void increment(View view) {
    quantity = quantity + 1;
    display(quantity);
}

/**
 * This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
    quantityTextView.setText("" + number);
}

/**
 * This method displays the given quantity value on the screen.
 */
private void display(int number) {
    TextView quantityTextView = (TextView) findViewById(R.id.amount_text_view);
    quantityTextView.setText("" + number);
}

I have edited this to also add part of the XML code that's being used:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_weight="1"
        android:background="#4CAF50"
        android:orientation="horizontal">

        <Button
            android:id="@+id/decrement_id"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginLeft="10dp"

            android:layout_weight="1"
            android:onClick="decrement"
            android:text="-" />

        <TextView
            android:id="@+id/quantity_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="0"
            android:textColor="#000000"
            android:textSize="20sp" />

        <Button
            android:id="@+id/increment_id"
            android:layout_width="48dp"
            android:layout_height="48dp"

            android:layout_weight="1"
            android:onClick="increment"
            android:text="+" />

        <TextView
            android:id="@+id/freeKick_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="FREE KICK"
            android:textColor="#000000"
            android:textSize="16sp" />

        <Button
            android:id="@+id/decrease_id"
            android:layout_width="48dp"
            android:layout_height="48dp"

            android:layout_weight="1"
            android:onClick="decrement"
            android:text="-" />

        <TextView
            android:id="@+id/amount_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="0"
            android:textColor="#000000"
            android:textSize="20sp" />

        <Button
            android:id="@+id/increase_id"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:onClick="increment"
            android:text="+" />

    </LinearLayout>

So here, what I am saying is that FREE KICK needs to be updated for both teams but only when a free kick awarded to each team. So when I hit the + button for Home Team, they get the Free Kick. Then if the Away Team get a free kick, then hit the + button for them. Hope that makes thing clearer! :)

Upvotes: 0

Views: 79

Answers (2)

Raymond Mutyaba
Raymond Mutyaba

Reputation: 950

Use separate onClick methods for each increment and decrement buttons.

( onClick="DecrementQuantity" , onClick="IncrementQuantity" , onClick="DecrementFreeKick" , onClick="IncrementFreeKick" , onClick="DecrementAmount" , onClick="IncrementAmount" )

You should not use the same onClick method for more than one button. Each onClick manages an action for each textview.

public void DecrementQuantity(View v) {
        findViewById(R.id.quantity_text_view).setText(Integer.toString(number));
    }

Upvotes: 0

Mick Mnemonic
Mick Mnemonic

Reputation: 7956

One way to avoid duplicating similar functionality in two methods is to add an another parameter to the method signature:

/**
 * This method displays the given quantity value on the screen.
 */
private void display(int number, @IntegerRes int viewId) {
    TextView quantityTextView = (TextView) findViewById(viewId);
    quantityTextView.setText("" + number);
}

Then, you'd use the method as follows:

public void increment(View view) {
    quantity = quantity + 1;
    display(quantity, R.id.quantity_text_view);
    display(quantity, R.id.amount_text_view);
}

Upvotes: 2

Related Questions