2016-06-22 5 views
0

이것은 즉, 방법 새로운 목소리() 후 5 ~ 10 초 후에 내 코드새로 고침 텍스트 뷰는 즉시

try { 
final double calcResult = CalcUtils.evaluate(textViewOutputScreen.getText().toString()); 

textViewOutputScreen.setText(Double.toString(calcResult)); //setting text to text view 

new voices().voice(calcResult); //this method takes about 5-10 seconds to execute 

} catch (Exception e) { 
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 
        textViewOutputScreen.setText("0"); 
} 
내가 두 번째 줄에 TextView로 설정 텍스트가 화면에 업데이트됩니다

입니다. 음성 (calcResult); 실행이 완료되었습니다.

나는 방법이 같은 몇 지연을 유지

  try { 
       Thread.sleep(700); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 

나는 방법은 내가 할 수있는 방법을 호출하기 전에 텍스트보기를 새로 고칠?

비슷한 질문을했지만 그 중 아무 것도 저에게 효과가 없었습니다. 당신이 main 스레드에서 코드를 실행하는 것처럼

답변

0

시도의 무효화를 당신이 시도 할 수있는 doen't 작품은 무거운 LOA를 두는 경우 new voices().voice(calcResult);

textViewOutputScreen.invalidate(); 

을 실행하기 전에보기 d on a AsyncTask

이것은 백그라운드에서 실행되며 Java Thread를 기반으로합니다. 당신은 당신이 단지 AsyncTask를을 만들고 매개 변수와 함께 실행

[Link to AsyncTask examples]

다음

private class VoiceTask extends AsyncTask<Double, Void, Void> { 
    protected void doInBackground(Double... stuff) { 
     //Do whatever voice does! 
     double calcResult = stuff[0]; 
     new voices().voice(calcResult); 


    } 

    protected void onPostExecute(Long result) { 
     //Is called when you are done, if you want to send stuff here 
     //you need to change this example, see the link below 
     //Not necessary to override (If I remember correctly) 
    } 
} 
class/ Activity

현재 당신의 중첩 클래스로 AsyncTask을 추가 할 수 있습니다. 값은 doInBackground에서 사용할 수 있습니다. 빠른 회신

try { 
     final double calcResult = CalcUtils.evaluate(textViewOutputScreen.getText().toString()); 

       textViewOutputScreen.setText(Double.toString(calcResult)); //setting text to text view 
       new VoiceTask().execute(calcResult); //CHANGED LINE 

      } catch (Exception e) { 
       Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 
       textViewOutputScreen.setText("0"); 
      } 
+0

덕분에 작동하지 않는, 무효화하지만 여전히 그럼 당신은 로버트가 말하는 것을 시도해야 – Redman

+1

를 작동하지 않는 것을 시도했다. 백그라운드 스레드에'new voice(). voice (calcResult);를 넣어보세요. 프로 팁에서 'AsyncTask'를 확인하고 있습니다. 그리고 백그라운드에서 무거운 물건을 다하는 것 – Mazze

+0

는 매력처럼 일했다, 고맙다. – Redman

0

이 보인다, 그리고 TextView.setText(...)event queue 지난 배치받을 때문에 그것은 new Voices().voice()가 실행을 완료 한 후 이제 업데이트됩니다.

당신은 따라 UI Thread에서 TextView를 업데이트 할 수 있습니다 :

YourActivity.this.runOnUiThread(new Runnable() { 
    public void run() { 
     textViewOutputScreen.setText(Double.toString(calcResult)); 
    } 
}); 

... 솔루션은 물론 백그라운드에서 장기 실행 작업을 실행하는 것입니다 훨씬 좋네요

+0

덕분에,이 시도하지만 여전히 응답을 – Redman