Link to home
Start Free TrialLog in
Avatar of Sol Chikuse
Sol Chikuse

asked on

How Do I reset Android Button Counter

How do I reset the counter on a button in android studio.

My code is-----

public class MainActivity extends AppCompatActivity {

    private FactBook mFactBook = new FactBook();
    private ArrayList<String> PreviousFacts = new ArrayList<>();
    public int ForwardIndexValue = 0;

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

        final TextView FunFact = (TextView) findViewById(R.id.funFact);
        final Button NextButton = (Button) findViewById(R.id.nextBtn);
        final Button BackButton = (Button) findViewById(R.id.backBtn);

        NextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String AnotherFunFact = mFactBook.getFunFact();
                FunFact.setText(AnotherFunFact);
                PreviousFacts.add(AnotherFunFact);
                ForwardIndexValue++;

            }
        });

        BackButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ForwardIndexValue > 0){
                    ForwardIndexValue--;
                    String HistoryFunFact = PreviousFacts.get(ForwardIndexValue);
                    FunFact.setText(HistoryFunFact);
                }
                else {
                    FunFact.setText("Click Show Next Fun Fact to read more interesting facts about us");

                }

            }

        });

    }
}
ASKER CERTIFIED SOLUTION
Avatar of Chris Harte
Chris Harte
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This would solve the OP's problem.