AsyncTask get() method not working properly

后端 未结 2 1959
青春惊慌失措
青春惊慌失措 2021-01-25 18:00

I am using the following code:

//(...)
translationTextView.setText(\"Searching for translation...\");
translationTextView.setVisibility(View.VISIBLE);

myAsyncTa         


        
相关标签:
2条回答
  • 2021-01-25 18:45

    As codeMagic points out, override the onPostExecute() method of your AsyncTask subclass, if you haven't already. It's executed on the main thread, you should update your UI there.

    0 讨论(0)
  • 2021-01-25 18:59

    It is called but then you call

    translationTextView.setText(translateTask.get() + "<BR>");
    

    and get() is a blocking call

    Try instead to set the text in your onPostExecute(). If I understand you correctly then something like this should give you what you want

       //(...)
            translationTextView.setText("Searching for translation...");
            translationTextView.setVisibility(View.VISIBLE);
    
            myAsyncTask = (MyAsyncTask) new MyAsyncTask().execute(someString);
    

    Then, assuming MyAsyncTask is an inner class of your Activity call translationTextView.setText() inserting whatever you are trying to return from doInBackground()

    0 讨论(0)
提交回复
热议问题