2012-03-06 12 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); 
} 
} 

私はアンドロイドに新しいです、この状況を処理するためのアンドロイドAPIの中で最善のアプローチはありますか?

答えて

2

あなたは、あなたのスレッドが同じ活動である場合は、これを使用することができます別のスレッド

youractivityname.this.runOnUiThread(new Runnable() { 

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

      } 
     }); 

からあなたのUI要素に触れ、次のコードを使用する必要があります。それ以外の場合は、アクティビティクラスオブジェクトを使用して上記のメソッドを実行する必要があります。
コードからdothis()を呼び出す必要があります。スレッドがその仕事をした後。スレッドが開始した直後にdothisメソッドを呼び出すと、スレッドがそのジョブを完了したかどうかが気になりません。

関連する問題