在TextView中更新数组

时间:2014-04-09 15:45:00

标签: java android arrays textview updating

我正在开发一个" app"这允许我在文本视图中打印数组,我需要能够在每次更改/更新元素时​​更新数组。但我无法做到。在使用

更改元素后,我尝试再次打印数组
printArrayToScreen();

但它直接在原始数组下打印和数组,这是有道理的,但我似乎无法更新数组,而不是每次都在原始数据下重新打印它。

这是我的java文件。

    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 个答案:

答案 0 :(得分:1)

似乎每次要打印数组时,只需将文本附加到textview中已有的内容即可。一个快速的解决方案是在附加新文本之前将textview的文本设置为空字符串。

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");
    }
}