I am trying to update a TextView at intervals of five seconds at a time after a button press, but when the application is run, the TextView will only display the last values of the loop that it is being run in.
Here is what I am trying:
private Runnable textUpdate = new Runnable() {
public void run() {
textOutput = (TextView)findViewById(R.id.textOutput);
textOutput.setText("Reading Number " + (rIndex+1) + "\n");
textOutput.append(sensorTime[rIndex] + "\n");
textOutput.append("Value: " + rPoint.getValue() + "\n");
}
};
…
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mHandler = new Handler();
TextView textOutput = (TextView) findViewById(R.id.textOutput);
textOutput.setText("Simulation output:\n");
Button button03 = (Button) findViewById(R.id.button03);
button03.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
for(int i = 0; i < index; i++) {
rIndex = i;
rPoint = dp[i];
mHandler.postDelayed(textUpdate, 5000);
}
}
});
}
Cannot understand why only the last value of the information I am trying to output is being displayed in the TextView.
Any help is appreciated.
The issue I see is on your last line of code:
You’re posting all your updates at 5000 milliseconds, each starting milliseconds apart (as fast as the phone can loop).
Try this instead: