Changing variable value in android / JAVA

I was trying to create a simple application where clicking tapb Button

increases the value of the variable notaps and reset Button

sets the value to 0. When I click tapb

it increases the value and click reset

resets it, but when I click again tabp

it increases from the previous value.

For example:

init value of notaps = 0;

      

I click tabp

3 times and notaps

value = 3

I click reset

and notaps

value = 0

I click tabp

3 times and notaps

value = 4

    Button tapb = (Button)findViewById(R.id.tapb);
    Button reset = (Button)findViewById(R.id.reset);


    tapb.setOnClickListener(
            new Button.OnClickListener(){
                int notaps = 0;
                @Override
                public void onClick(View v) {
                    TextView taps = (TextView)findViewById(R.id.taps);
                    notaps++;
                    taps.setText(String.valueOf(notaps));

                }
            }
    );

    reset.setOnClickListener(
            new Button.OnClickListener() {

                @Override
                public void onClick(View v) {
                    TextView taps = (TextView) findViewById(R.id.taps);
                    int notaps=0;
                    taps.setText(String.valueOf(notaps));

                }
            }
    );

      

+3


source to share


2 answers


First, you have 2 int

named instances notaps

that have nothing to do with each other. Your reset button is not setting the correct variable notaps

.

Here's a snippet that should work.



    private int notaps;

    Button tapb = (Button)findViewById(R.id.tapb);
    Button reset = (Button)findViewById(R.id.reset);
    TextView taps = (TextView)findViewById(R.id.taps);

    tapb.setOnClickListener(
        new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                notaps++;
                taps.setText(String.valueOf(notaps));
            }
        }
    );

    reset.setOnClickListener(
        new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                notaps = 0;
                taps.setText(String.valueOf(notaps));
            }
        }
    );

      

+4


source


Try declaring your notaps variable globally where every user declares buttons for you declaring your notaps variable there, that's what I mean.



Button tapb = (Button)findViewById(R.id.tapb);
Button reset = (Button)findViewById(R.id.reset);
int notaps = 0; // declare variable globally

tapb.setOnClickListener(
        new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                TextView taps = (TextView)findViewById(R.id.taps);
                taps.setText(String.valueOf(++notaps));

            }
        }
);

reset.setOnClickListener(
        new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                TextView taps = (TextView) findViewById(R.id.taps);                    
                taps.setText(String.valueOf(notaps=0));

            }
        }
);

      

-1


source







All Articles