Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arrays are used to store data in a structured fashion.
- // It's basically a list of items, any kind of items organized in a manner,
- // such that each item or element within it can be accessed by using a number.
- // The number is known as the index of the element and
- // it's simply a counter of the elements that starts at zero.
- // And is incremented by one for each additional element in the array.
- //The items in the array can be any kind of data type or object.
- // Our app will use an array of string objects because each fact will be
- // stored as text in a string.
- public class FunFactsActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_fun_facts);
- final TextView factLabel = (TextView) findViewById(R.id.factTextView);
- Button showFactButton = (Button) findViewById(R.id.showFactButton);
- View.OnClickListener listenerName = new View.onClickListener(){
- @Override
- public void onClick(View view) {
- String[] facts = {
- "Ants stretch when they wake up in the morning.",
- "Ostriches can run faster than horses.",
- "Olympic gold medals are actually made mostly of silver.",
- "You are born with 300 bones; by the time you are an adult you will have 206.",
- "It takes about 8 minutes for light from the Sun to reach Earth.",
- "Some bamboo plants can grow almost a meter in just one day.",
- "The state of Florida is bigger than England.",
- "Some penguins can leap 2-3 meters out of the water.",
- "On average, it takes 66 days to form a new habit.",
- "Mammoths still walked the earth when the Great Pyramid was being built." };
- String fact = "";
- Random randomGenerator = new Random ();
- int randomNumber = randomGenerator.nextInt(facts.length);
- fact = facts[randomNumber];
- factLabel.setText(fact);
- }
- };
- showFactButton.setOnClickListener(listenerName);
- }
- }
- //-----------------------------------------------------------------------------------------------------------------------------------------
- public class FunFactsActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_fun_facts);
- final TextView factLabel = (TextView) findViewById(R.id.factTextView);
- Button showFactButton = (Button) findViewById(R.id.showFactButton);
- View.OnClickListener listenerName = new View.onClickListener(){
- @Override
- public void onClick(View view) {
- String[] facts = { // Datatype[] nameOfArray
- "Ants stretch when they wake up in the morning.", //index 0
- "Ostriches can run faster than horses.", //index 1
- "Olympic gold medals are actually made mostly of silver.", //index 2
- "You are born with 300 bones; by the time you are an adult you will have 206.", //index 3
- "It takes about 8 minutes for light from the Sun to reach Earth.", //index 4
- "Some bamboo plants can grow almost a meter in just one day.", //index 5
- "The state of Florida is bigger than England.", //index 6
- "Some penguins can leap 2-3 meters out of the water.", //index 7
- "On average, it takes 66 days to form a new habit.", //index 8
- "Mammoths still walked the earth when the Great Pyramid was being built." }; //index 9
- String fact = "";
- Random randomGenerator = new Random ();
- int randomNumber = randomGenerator.nextInt(facts.length); // "nameOfArray.length" uses the # of elements in the array instead of the hardcoded element like "10"
- fact = facts[randomNumber]; // this syntax is saying hey, facts array, give me the element at the index a random number.
- // Duh:
- // Now, we could have plugged in a number here instead of the random number variable
- // like zero would give us the first number in the array. //fact = facts[0]
- // But that would always give us the same element.
- factLabel.setText(fact);
- }
- };
- showFactButton.setOnClickListener(listenerName);
- }
- }
- //https://teamtreehouse.com/library/build-an-android-app/coding-the-fun-facts/introduction-to-arrays
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement