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

Aaron Arkie
Aaron Arkie
5,345 Points

Array Madness Extra Credit in Android Track

Can anyone assist me in actually completing this objective. I want to loop through an array of strings and print that number to the text view each time i press the button. The first array should increment by one going forwards and the second array should do the opposite. Thanks in advance! Here is my code:

package com.arkman.arraymadness;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class ArrayMadnessActivity extends ActionBarActivity {

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

        final TextView firstView = (TextView) findViewById(R.id.forwardsArrayView);
        final TextView secondView = (TextView) findViewById(R.id.backwardsArrayView);

        final Button button = (Button) findViewById(R.id.myButton);

        final View.OnClickListener listener = new View.OnClickListener() {
            public void onClick(View v) {

                String[] firstTvArray = {"1", "2", "3", "4", "5"};
                String[] secondTvArray = {"5", "4", "3", "2", "1"};
                String firstLoop = "";
                String secondLoop="";

                for(String i:firstTvArray){  

                    // I have trouble here

                    firstLoop = i;
                    firstView.setText(firstLoop);


                }

            }

        };

        button.setOnClickListener(listener);


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_array_madness, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 Answer

From what I understand when you press the button, firstTvArray should print the elements from left to right i.e. in increasing index and secondTvArray from right to left in decreasing index. Is that correct?

You could do something like this I suppose

for(String i:firstTvArray){  
                    firstLoop = i;
                    firstView.setText(firstLoop);
}

for (int i = secondTvArray.length-1; i >= 0; i--)
{
     secondLoop = secondTvArray[i];
     secondView.setText(secondLoop);
}

Hope this helps

Aaron Arkie
Aaron Arkie
5,345 Points

Unfortunately it does not work. I get it to display the first number, and with your code of the second array it displays the last number. I think my problem is where i set the button to be heard if it was clicked. once it reaches that then the code is finished? I think i remember Ben doing this in the crystal ball app so i will wait until then but thanks for your attempt!