1

I am working on an "app" that allows me to print an array in a text view and I need to be able to update the array every time an element is changed/updated. But I can't get it right. I tried to print the array again after an element is changed using

printArrayToScreen();

but it prints and array directly under the original array, which makes sense, but I can't seem to update the array without reprinting it under the original each time.

here is my java file.

    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=15;

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();


    }
//prints the array to the screen
    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]+" ");
            }
        tv.append("\n");
        }
        }


    //sets up the array 
    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;//if the counter is equal to 0 it does nothing
                else
                    counter--;//subtracts from counter to traverse the array
            }



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

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;//adds to counter so it can traverse the array
            }



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

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                int a=1;
                array[a][counter]="12";//just for testing purposes
                         //I think I need an update method here after I insert it into the array                
            }


        });


        }

    @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;
    }

}
1
  • Instead of just using tv.append alone, you can first clear the text of the textview, then start appending the new text. Commented Apr 9, 2014 at 15:47

1 Answer 1

1

It seems that everytime you want to print the array, you are simply appending the text to whatever is already in the textview. A quick solution is to just set the text of the textview to an empty string before appending your new text.

private void printArrayToScreen() {
    tv.setText(""); //Before printing your data clear the textview
    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            tv.append(array[i][j]+" ");
        }
        tv.append("\n");
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@user3369073 Has this helped you?
Ok, what didn't work about it? I've just tested this and it worked fine.
Actually it did work! I forgot to call printArrayToScreen() that was the problem! Thanks for the help!

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.