【问题标题】:Updating an array in TextView在 TextView 中更新数组
【发布时间】:2014-04-09 15:45:00
【问题描述】:

我正在开发一个“应用程序”,它允许我在文本视图中打印一个数组,并且我需要能够在每次更改/更新元素时​​更新数组。但我无法正确处理。在使用

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

}

【问题讨论】:

  • 除了单独使用tv.append之外,还可以先清除textview的文本,然后开始追加新文本。

标签: java android arrays textview updating


【解决方案1】:

似乎每次您想打印数组时,您只需将文本附加到文本视图中已有的内容。一个快速的解决方案是在添加新文本之前将 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");
    }
}

【讨论】:

  • @user3369073 这对您有帮助吗?
  • 好的,有什么不好的?我刚刚对此进行了测试,效果很好。
  • 实际上它确实有效!我忘记调用 printArrayToScreen() 了,这就是问题所在!感谢您的帮助!
  • 没问题,很乐意提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多