2013-12-08 84 views
0

我正在使用Jsoup解析網站的一部分,然後將其放入一個字符串。我想將此字符串可視化爲textView,但由於只有創建了textView的線程才能修改它,因此我需要將字符串的值傳遞給主線程。怎麼樣?如何將字符串的值從線程轉移到另一個線程?

這是代碼:(忽略tabhost東西)

public class NewsAndAnnouncements extends Activity { 

    TabHost host; 
    FlyOutContainer container; 
    Button bttoggle; 
    Button bt1; 
    String loggity; 
    TextView tv1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     this.container = (FlyOutContainer) this.getLayoutInflater().inflate(
       R.layout.newsandannouncements, null); 
     this.setContentView(container); 

     host = (TabHost) findViewById(R.id.tabhost); 
     host.setup(); 

     TabSpec specs = host.newTabSpec("TAGGITY EINZ"); 
     specs.setContent(R.id.tab1); 
     specs.setIndicator("News"); 
     host.addTab(specs); 

     specs = host.newTabSpec("TAGGITY ZWEI"); 
     specs.setContent(R.id.tab2); 
     specs.setIndicator("Notices"); 
     host.addTab(specs); 

     specs = host.newTabSpec("TAGGITY DREI"); 
     specs.setContent(R.id.tab3); 
     specs.setIndicator("Events"); 
     host.addTab(specs); 

     tv1 = (TextView) findViewById(R.id.textView1); 

     /* 
     * bttoggle = (Button) findViewById(R.id.bttoggle); bt1 = (Button) 
     * findViewById(R.id.Button1); 
     * 
     * bttoggle.setOnClickListener(new OnClickListener() { 
     * 
     * @Override public void onClick(View v) { // TODO Auto-generated method 
     * container.toggleMenu(); } }); 
     * 
     * bt1.setOnClickListener(new OnClickListener() { 
     * 
     * @Override public void onClick(View v) { // TODO Auto-generated method 
     * container.toggleMenu(); } }); 
     */ 

     Thread newsThread = new Thread() { 
      public void run() { 

       Document doc = null; 
       try { 
        doc = Jsoup 
          .connect(
            "http://acs.bg/Home/About_ACS/News_and_Events/News.aspx") 
          .get(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       Elements myin = doc.getElementsByClass("news_list"); 
       loggity = myin.toString(); 


         Log.i("ELEMENTS HTML", loggity); 

      } 
     }; 
     newsThread.start(); 

     tv1.setText(loggity); 

    } 

} 
+0

的代碼發佈 – Raghunandan

+0

TextView的沒有想象的任何事情是錯誤的。它是空的 - 因此我得出結論,字符串最後是空的。 textView與我在線程之外創建的任何其他字符串一起使用。我知道myin.toString();方法產生值,因爲我可以看到日誌中的字符串。 – NoodlesFromHell

+0

考慮使用AsyncTask而不是「原始」線程:http://developer.android.com/reference/android/os/AsyncTask.html – isnot2bad

回答

4

嘗試使用AsyncTask而不是主題。要修改您的UI線程中的視圖,請在您的活動中使用runOnUiThread()方法。

runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     tv1.setText("..."); 
    } 
}); 
+0

AsyncTask很好,但在UI線程上運行它是一個壞主意,因爲在任務運行時UI將被凍結! – isnot2bad

+0

爲了澄清,'runOnUiThread'僅用於更改textview的文本,而不是用於AsyncTask – bbuecherl

+0

嗯,沒關係! ;) – isnot2bad

1

使用一個的AsyncTask來代替原始主題:

new AsyncTask<URL, Object, Document>() { 
    protected Document doInBackground(URL... urls) { 
     // parse URL and return document 
    } 

    protected void onPostExecute(Document result) { 
     // this runs in UI thread 
     // show document in UI 
    } 
}).execute(myURL); 
1

有兩種方法d IT-

1) - 使用的AsyncTask

2) - 使用處理器

Thread newsThread = new Thread() 
{ 
      public void run() 
      { 
       Document doc = null; 
       try { 
        doc = Jsoup 
          .connect(
            "http://acs.bg/Home/About_ACS/News_and_Events/News.aspx") 
          .get(); 
       } 
       catch (IOException e) 
       { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

       Elements myin = doc.getElementsByClass("news_list"); 
       loggity = myin.toString(); 

       mHandler.post(new Runnable() 
       { 
        @Override 
        public void run() 
        { 
          try 
          { 
            tv1.setText(loggity); 
          } catch (Exception e) 
          { 
            e.printStackTrace(); 
          } 
        } 
      }); 
      Log.i("ELEMENTS HTML", loggity); 
      } 
     }; 
     newsThread.start(); 

您可以初始一開始就讓漢德爾化。

+0

謝謝。我已經找到了答案,但我會嘗試這個,也許也試試你的方式!再次感謝 :) – NoodlesFromHell

1

嘗試此示例代碼,不知道這是更好的辦法:

public class MainThread { 


public static void main(String args[]) { 
    Thread2 t2 = new Thread2(); 
    Thread nextThread = new Thread(t2); 
    nextThread.start(); 

    try { 
     nextThread.join(); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    System.out.println(); 
    System.out.println(t2.getStr()); 

} 

private static class Thread2 implements Runnable{ 
    private String str; 

    @Override 
    public void run() { 
     setStr("T2 Thread String"); 

    } 
    public String getStr() { 
     return str; 
    } 
    public void setStr(String str) { 
     this.str = str; 
    } 

} 


}