0

Sorry for the coding! I am fairly new at developing for the android and I am having trouble printing my array into the TextView the way I would like. I know some java but I am sure my coding is "newbie-ish" with a lot of bad coding. The purpose of this exercise is to help me understand Android and java better.

Here is my code. package com.example.taplature;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
int counter=0;
Button prev;
Button a;
Button next;
TextView tv;
int row=6;
int col=6;

String[][] array = new String [row][col];



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

        prev=((Button) findViewById(R.id.prev));
        a=((Button) findViewById(R.id.printA));
        next=((Button) findViewById(R.id.next));
        tv=((TextView) findViewById(R.id.arrayTv));

        setButtonOnClickListeners();
        setUpArray();
        printArrayToScreen();


    }

    private void printArrayToScreen() {
        // TODO Auto-generated method stub

        for(int i=0;i<row;i++)
            for(int j=0; j<col;j++)
            tv.append(array[i][j]);
    }



    private void setUpArray() {
        // TODO Auto-generated method stub
        for(int i=0; i<row;i++)
            for(int j=0; j<col;j++)
            array[i][j]="-";
    }

    private void setButtonOnClickListeners() {
        // TODO Auto-generated method stub
        prev.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if(counter==0)
                    counter=0;
                else
                    counter--;
            }



        });
        next.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
            }



        });
        a.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int a=1;
                array[a][counter]="12";
            }


        });


        }

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

}

I am trying to print out a small 2d array of size 6x6 with "-" as the elements like this.

------
------
------
------
------
------

But on the android emulator it is just

--------------------
----------------

which I am assuming it just reaches the end of the screen and wraps the content?

The purpose of this app is to print out the array and the user can press next/prev to go to each column and if he presses "a" then it will put a value in the array although I have not made it far enough past printing the array on the screen properly. Also I chose a string array because I will be using numbers as well as letters.

If you guys/gals could point me in the right direction I would greatly appreciate any help

1 Answer 1

2

printArrayToScreen() needs to print a newline at the end of each row:

private void printArrayToScreen() {
    for(int i=0;i<row;i++) {
        for(int j=0; j<col;j++)
            tv.append(array[i][j]);
        tv.append("\n"); // Append newline after every row
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I guess I am a little rusty with my thought processes!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.