Solved

How Do I reset Android Button Counter

Posted on 2016-10-02
2
34 Views
Last Modified: 2016-10-22
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");

                }

            }

        });

    }
}
0
Comment
Question by:Sol Chikuse
  • 2
2 Comments
 
LVL 16

Accepted Solution

by:
Chris Harte earned 500 total points (awarded by participants)
ID: 41826187
Set it to zero inside the onCreate method. Every time it is created it will be set to zero

       super.onCreate(savedInstanceState);
       setContentView(R.layout.content_main);
       public int ForwardIndexValue = 0;

Open in new window

0
 
LVL 16

Expert Comment

by:Chris Harte
ID: 41855075
This would solve the OP's problem.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
Introduction This article is a continuation of Part-1 as evident from the title. Please visit this page (http://www.experts-exchange.com/articles/18706/Backing-up-personal-data-on-your-Android-device-Part-1.html) to read the earlier Part -1. If y…
This video is in connection to the article "The case of a missing mobile phone (https://www.experts-exchange.com/articles/28474/The-Case-of-a-Missing-Mobile-Phone.html)". It will help one to understand clearly the steps to track a lost android phone.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now