2015-01-08 64 views
2

我有一个的EditText和一个TextView的,我想更新我在每个迭代的TextView(ConsoleWindow在一个循环中运行,它会从一个处理函数中调用,从而在UIthread正在运行)。安卓的TextView没有更新

的问题是,我的TextView只在第一轮被更新,然后持续了运行时的其余部分中的第一项(虽然dataString是一个不同每轮):

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

我已经尝试过tv.invalidate()和tv.postInvalidate(),但是这并没有产生效果。请有人帮助我吗?

+2

你难道没有拿到TextView的更新,您可以创建一个新的 – tyczj

+0

问题是,我不能把'新TextView'的if语句,否则'tv.setText(...)'无法执行(因为其外部if语句的) – blackst0ne

+2

你应该使用'新TextView'任何地方,因为这就是一个'NEW' TextView的,而不是你正在更新 – tyczj

回答

1

把电视全局变量。

private TextView tv; 

在此之后,在你的 “的onCreate()” 方法:

tv = new TextView(getApplicationContext()); 

然后:

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

请核实过,如果dataString有一些文字,像这样的东西

Log.d(TAG , "dataString: " + dataString + "with first time? " + first2.toString()); 

试着通过t他setContentView(布局);在if语句之外。因为我无法理解你为什么需要这个。

LinearLayout layout = new LinearLayout(getApplicationContext()); 
    setContentView(layout); 
+0

耶士终于成功了!我无法相信我在这么简单的事情上投入了太多时间^^谢谢! – blackst0ne

+0

是第一个解决方案吗? :) – rguerra

+0

nope,已经有一个;但是那个不适用于这些声明 – blackst0ne

1

first2false,你只是创建一个新的的LinearLayoutlayout然后不充气layout,你是直接添加的TextViewtvlayout。这就是为什么Textvie瓦特是不可见的。

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); 
    } 
} 
+0

感谢你的帮助,但还是同样的问题-.- – blackst0ne