AsyncTask OnPostExecute not updating TextView

2019-01-26 19:54发布

I have an AsyncTask running. I have a TextView that I mimic the message a Toast initially produces. I want to clear the TextView upon success in OnPostExecute but it not doing so. The task complete Toast works fine. How do I set the TextView in the OnPostExecute to blank? The user is still on the display screen where the TextView is.

Code is as follows for an error condition:

@Override
protected void onPostExecute(Void result) 
 { FetchingImage=0;
  if(webLoadError>0)
   {
    TextView text = (TextView) findViewById(R.id.textView2);
    String temp=" ";
    text.setText(temp);
    Toast.makeText(getApplicationContext(), "Image not available from the internet.\nDefault or last image loaded.\nTry again later.",Toast.LENGTH_LONG).show();
    }  
  }

2条回答
孤傲高冷的网名
2楼-- · 2019-01-26 20:18

Try something like:

((TextView) findViewById(R.id.textView2)).setText("");

EDIT:

try making a variable outside the onCreate like TextView text; and then inside the onCreate put: text = (TextView) findViewById(R.id.textView2);

and then just put text.setText(""); inside the onPostExecute method.

See if that works.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-26 20:27

I know this thread is old, but I think I found the solution (at least it worked for me), perhaps it help others:

I had a view with 3 textviews with default values receiving values from a webservices from "doInBackground" method of a AsyncTask and later changing the text in them on "onPostExecute" method. The issue was that only one of the three textviews was showing the new text from the ws in the first execution of code (it's a simple application implementing the Zxing barcode reader reading a barcode from a product and obtaining the price from a webserver, anyway), the next executions (after the app is opened) was updating the three textviews normally.

So I noticed that the only textview that was updating its value in the first execution had its parameter "android:textIsSelectable" = true, the other two was false. Bingo, changing this parameter to true in the other 2 textviews solved the issue.

查看更多
登录 后发表回答