Reputation: 3264
I got one EditText and one TextView, and I want to update my TextView in each Iteration (ConsoleWindow is running in a loop; it gets called from a handler and thus is running on the UIthread).
The Problem is that my TextView only gets updated in the first round, and then it keeps the first entry for the rest of the runtime (although the dataString
is a different one in each round):
private void ConsoleWindow(String dataString) {
LinearLayout layout = new LinearLayout(getApplicationContext());
if (first2) { //first2 is true when application is launched
// ONLY SET LAYOUT AND EDITTEXT IN FIRST RUN TO SAVE CAPACITY
// LINEAR LAYOUT
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.parseColor("#000000")); // black
// EDITTEXT
EditText et = new EditText(getApplicationContext());
et.setHint("Enter Command");
layout.addView(et);
first2 = false;
}
// TEXTVIEW
TextView tv = new TextView(getApplicationContext());
tv.setText(dataString); // KEEPS THE SAME UNTIL THE 1ST ROUND
layout.addView(tv);
}
I already tried tv.invalidate() and tv.postInvalidate(), but that didn't have an effect. Could someone help me please?
Upvotes: 2
Views: 2355
Reputation: 2555
Put tv a global variable.
private TextView tv;
After this, In your "onCreate()" method:
tv = new TextView(getApplicationContext());
And then:
private void ConsoleWindow(String dataString) {
LinearLayout layout = new LinearLayout(getApplicationContext());
if (first2) { //first2 is true when application is launched
// ONLY SET LAYOUT AND EDITTEXT IN FIRST RUN TO SAVE CAPACITY
// LINEAR LAYOUT
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.parseColor("#000000")); // black
// EDITTEXT
EditText et = new EditText(getApplicationContext());
et.setHint("Enter Command");
layout.addView(et);
first2 = false;
}
// TEXTVIEW
tv.setText(dataString); // KEEPS THE SAME UNTIL THE 1ST ROUND
layout.addView(tv);
}
Please verify too, if dataString has some text, with something like this
Log.d(TAG , "dataString: " + dataString + "with first time? " + first2.toString());
Try to pass to the setContentView(layout); outside the if statement.Because I can't understand well Why you need this.
LinearLayout layout = new LinearLayout(getApplicationContext());
setContentView(layout);
Upvotes: 1
Reputation: 2737
When first2
is false
, you are simply creating a new LinearLayout layout
and then without inflating the layout
, you are directly adding TextView tv
to layout
. That's why Textview is not visible.
private void ConsoleWindow(String dataString) {
LinearLayout layout;
TextView tv;
EditText et;
if (first2) {
layout = new LinearLayout(getApplicationContext());
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.parseColor("#000000"));
// EDITTEXT
et = new EditText(getApplicationContext());
et.setHint("Enter Command");
layout.addView(et);
tv = new TextView(getApplicationContext());
layout.addView(tv);
first2 = false;
}
if(tv != null) {
tv.setText(dataString);
}
}
Upvotes: 1