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 trialaybars tokta
4,491 PointsThere is only one change in FactText
When we create our mFact Class random number variable is generated once , and there is no indication of changing randomNumber in onClick method so that after running the app , the fact changes only after first tapping, additional tapping does not changes the facttext in my app
Can you help me with that?
aybars tokta
4,491 Pointspackage com.tokta.aybars.funfacts;
import android.app.Activity;
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 FunFactActivity extends Activity {
private FactBook mFact = new FactBook();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fun_fact);
//declare our view variables
final TextView factLabel = (TextView) findViewById(R.id.BilgiKutusu);
Button showFactButton = (Button) findViewById(R.id.DegistirTusu);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String fact = mFact.GetFact();
factLabel.setText(fact);
}
};
showFactButton.setOnClickListener(listener);
}
}
/////////////////////////////////////////////////////////// Other class
package com.tokta.aybars.funfacts;
import java.util.Random;
/**
* Created by aybars on 19.09.2014.
*/
public class FactBook {
String[] mFacts= {"GYTE hakkimi yedi","GYTE ye kafam girsin","Dandik laptop verdi","ingilizce belgemi begenmiyor"};
//Randomly select a fact
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(mFacts.length);
public String GetFact(){
String fact = mFacts[randomNumber];
return fact;
}
}
2 Answers
Stone Preston
42,016 Pointsyou need to put your random number generation code inside the getFact method:
public class FactBook {
String[] mFacts= {"GYTE hakkimi yedi","GYTE ye kafam girsin","Dandik laptop verdi","ingilizce belgemi begenmiyor"};
//you had your number generation code here, this is not where it belongs
public String GetFact(){
//move your number generation code in here
//Randomly select a fact
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(mFacts.length);
String fact = mFacts[randomNumber];
return fact;
}
}
currently you have it ouside the method, so it only gets generated one time. By moving it into the method, a new number will get generated every time the method is called
also note that its conventional to name methods starting with a lower case letter. you started yours with an uppercase letter. This style is usually reserved for class names. Technically its not wrong, its just unconventional.
aybars tokta
4,491 PointsThanks a lot man!
Stone Preston
42,016 PointsStone Preston
42,016 Pointscan you post your code?