Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (2014) Improving Our Code Simple Refactoring: Using a Class

Refactoring code

I am currently creating the fact finder app and i have now refactored my code . The app is now only showing one fact and when i click on the button "show another fun fact" it only shows the word fact , i believe i am going wrong some where so can someone pleasw helo me solve this issue and also explain where i am going wrong.

Here is my code for the FunFactsActivity.java

package com.example.curtis.funfacts;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Random;

public class FunFactsActivity extends AppCompatActivity {

private FactBook mFactBook = new FactBook();

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

    //declare are View variables and assign the views from the layout file
    final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    Button showFactButton = (Button) findViewById(R.id.showFactButton);
    View.OnClickListener Listener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            String fact = mFactBook.getFact();
            //update the label with our dynamic fact
            factLabel.setText("fact");

            mFactBook.mFacts[0] = "Pigs are cool";
        }
    };
    showFactButton.setOnClickListener (Listener);

}

}

Here is my code for the FactBook.java

package com.example.curtis.funfacts;

import java.util.Random;

public class FactBook {

// member variable ( properties about the object )

public String[] mFacts ={ "Have a bath everyday.", "Read a book once a month.", "Always strive to be the best.", "Have a piece of fruit everyday.", "Spend time with family.", "Always exercise.", "Don't waste your time with rubbish.", "Value your time.", "Take risks.", "Don't be afraid to make mistakes.", "Don't be afraid to ask for help.", "Have morals and values."};

// method (abilities : things the object can do )
   public String getFact(){
//update fct label with new fact
       String fact = "";


   // randomly select a fact
   Random randomGenerator = new Random();//construct a new random number generator
   int randomNumber = randomGenerator.nextInt(mFacts.length);
    //convert random number to text fact
   fact = mFacts[randomNumber];

   return fact;
   }

}

2 Answers

Be careful when setting text with a variable.

In your onClick: method you set the text with this code:

String fact = mFactBook.getFact();

factLabel.setText("fact");

Remember that 'fact' is a variable and therefore doesn't need quotation marks around it like string literals.

Try this:

String fact = mFactBook.getFact();

factLabel.setText(fact);

Ok thanks that has worked however do i need to include the follwing import functions

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.Random;

faraz
faraz
Courses Plus Student 21,474 Points

Curtis, the IDE marks which import functions are necessary. You have to include an import function unless it's grayed out. If it's grayed out, it's safe to delete.