Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build an Interactive Story App (Retired) Finishing the User Interface Ending the Story

Angelos Hadjiphilippou
Angelos Hadjiphilippou
1,600 Points

Tried adding a 3rd button for 2 specific pages but cannot detect which page

In loadPage(int choice) i added an if(choice == 6) but it doesn't detect it. However, if i switch that to choice == 0 it works and my 3rd button appears.

Here is the full IF statement

int mCurrentPageNumber = choice; if(mCurrentPageNumber == 6) { mChoice3.setText(mCurrentPage.getChoice3().getText()); } else if(mCurrentPageNumber == 0) { mChoice3.setText(mCurrentPage.getChoice3().getText()); } else { mChoice3.setVisibility(View.INVISIBLE); }

page(0) shows the 3rd button, but page(6) doesn't. Anyone see the error?

8 Answers

Edmond Cotterell
Edmond Cotterell
8,920 Points

How many pages does ur app have? because if u have 6 pages, remember its index is 5, so your conditional statement should be:

if(choice == 5) //to check for the 6th page { //do something }

and to add the buttons to two pages it would be:

if(choice == 5 || choice == 0) { //do something }

Hope that was helpful.

Angelos Hadjiphilippou
Angelos Hadjiphilippou
1,600 Points

Hi Edmond

It has 11 pages and that one is correctly indexed as number 6.

here is the array where it refers to:

```mPages[6] = new Page( R.drawable.internet_connection, "What is your internet connection at your business?", new Choice("3G", 8), new Choice("ADSL or Wifi", 9), new Choice("No Internet", 10));

Added your suggestion with  ```if(choice == 6 || choice == 0)``` but still doesn't work for number 6
Edmond Cotterell
Edmond Cotterell
8,920 Points

since your program does not crash its probably a logic error.. so right before your if statement put a Toast that prints out the value of mCurrentPageNumber :

Toast.makeText(this, mCurrentPageNumber,Toast.LENGTH_LONG).show();

if(mCurrentPageNumber == 6) { }

what value are you getting? if its not 6, then its either not getting your choice or the value is being modified before it gets to the if statement.

Angelos Hadjiphilippou
Angelos Hadjiphilippou
1,600 Points

Thank you Edmond, it`s really helping to shed some light into this. However, there is ghosts in the machine.

Toast was crashing so i had to convert to string. I did this: String mCurrentPageNumber = String.valueOf(choice); Toast.makeText(this, mCurrentPageNumber, Toast.LENGTH_LONG).show();

Outputs correctly and guess what. The questionable page is indeed ... 6

Had to change the if statement also to go back to a working page(0) with 3rd button, but its still a noshow on page(6). if(mCurrentPageNumber == "6" || mCurrentPageNumber == "0") {

And after 10hrs of trying to figure this out, i am now officially ready to... :P

Edmond Cotterell
Edmond Cotterell
8,920 Points

ur welcome...I was really hoping u would have found the bug... I'm not sure but have you checked to see that 'mCurrentPage.getChoice3().getText()' is returning a string for both when 'mCurrentPageNumber = 6 & mCurrentPageNumber = 0' It could be the problem.

'''java if(mCurrentPageNumber == 6) {
mChoice3.setText(mCurrentPage.getChoice3().getText()); } '''

Angelos Hadjiphilippou
Angelos Hadjiphilippou
1,600 Points

SOLUTION:

The problem was that after page(0) the button view was set to INVISIBLE.

in the IF statement i added the following which solved the problem by overriding this setting: mChoice3.setVisibility(View.VISIBLE);

Thank you Edmond. Your help was priceless ;)