Reputation: 55
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;
}
}
Upvotes: 1
Views: 255
Reputation: 3445
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");
}
}
Upvotes: 1