java - 更新 TextView 中的数组

标签 java android arrays textview updating

我正在开发一个“应用程序”,它允许我在 TextView 中打印一个数组,并且我需要能够在每次更改/更新元素时​​更新数组。但我做不对。我尝试在使用

更改元素后再次打印数组
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;
    }

}

最佳答案

似乎每次您想打印数组时,您只是将文本附加到 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");
    }
}

关于java - 更新 TextView 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22967635/

相关文章:

java - 像素阵列的旋转

java - 如何计算二维数组中每一列的平均值?

java - ArrayList 调整当前底层数组的大小或创建一个新数组?

android - 房间数据库 onConflict = OnConflictStrategy.REPLACE 不起作用

java - 空 Activity 中缺少操作栏

Android FFmpegMediaPlayer - MediaPlayer 错误(0, 0)

java - 如何将 kotlin 集合作为可变参数传递?

java - JBOSS 5.1 可以在 JDK 1.4 中运行吗?

java - 从Powershell调用jar文件并处理返回值

java - 这个加密代码有什么问题吗?