2012-03-06 39 views
0

在我的应用程序,我使用肥皂Web服务调用的基础上,Web服务调用的回复我要显示一些消息,要调用主THEAD的方法从子线程

但回复后我不能能够从衍生的子线程为此

因此,如何要回主线程和我有

希望这是明确的答复后,显示此..帮助我如何实现我的要求

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

    String threadName = Thread.currentThread().getName(); 
    // There will be delay in this statement while fetching a data from webservice 
    String returnfromWebservice = webservice(xmlDetails, "generateid"); 
    Log.v("returnfromWebservice",returnfromWebservice); 
    if( ! returnfromWebservice.equalsIgnoreCase("nil")){ 
     gotid = returnfromWebservice; 
     gotReply=true; 
    // dothis();// I could able to do this because this method contains widgets 
    // I am gettin the error : Only the original thread that created a view hierarchy can touch its views. 
    //I understand this is because childthread has no controls on widget 
    /**Suggest me how to get back to main thread*/ 
    } 
    }}; 
t1.start(); 
dothis();// so i am doin here after the completion of it 


} 

public void dothis{ 
if(gotReply){ 
    idtext.setText(gotid); 
    genId.setEnabled(false); 
    Toast.makeText(WelcomeScorer.this, "Generated ", 500).show(); 
} 
else{ 
    Toast.makeText(WelcomeScorer.this, "Try Once More ", 500).show(); 
    idtext.setText(gotid); 
} 
} 

我是新来的android,是否有任何最佳的方法在android api来处理这种情况?

回答

2

你应该使用下面的代码从另一个线程触摸你的UI元素

youractivityname.this.runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 

      } 
     }); 

如果你的线程是在同一个活动,你可以用这个。否则,您应该使用您的活动类对象来运行上述方法。
从你的代码中你应该调用dothis();线程完成工作后。在线程启动后,它会立即调用dothis方法,而不会在意线程是否完成了它的工作。