Solved

How Do I reset Android Button Counter

Posted on 2016-10-02
2
31 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)
Comment Utility
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
Comment Utility
This would solve the OP's problem.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I tend toward trying the newest hardware and software.  Thiss sometimes works out to my benefit, and sometimes not.  Because I downloaded and installed Android 5.x (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.htm…
A short article about problems I had with the new location API and permissions in Marshmallow
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

9 Experts available now in Live!

Get 1:1 Help Now