AsyncTask的 - 执行后,如何更新看法?看法、AsyncTask

2023-09-13 01:30:47 作者:Monstar 7 °

在一个活动的OnCreate()事件,我已经开始了的AsyncTask从数据库中检索产品数据。在此之后已成功完成,我怎么能更新显示?

In the onCreate() event of an Activity, I have started an AsyncTask to retrieve Product data from a database. After this has been completed successfully, how can I update the display?

元code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.venueviewbasic);
            (..)
    new GetProductDetails().execute();

class GetProductDetails extends AsyncTask<String, String, String> {

    protected String doInBackground(String... params) {

        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                // Check for success tag
                int success;
                try {
                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("id", vid));
        (.. retrieve and parse data and set new textview contents ..)

在textviews等没有得到更新。但是。

The textviews etc. don't get updated however.

推荐答案

如果你想在那么完整的过程后,从异步更新视图 你可以使用

If you want to update the view from async after complete process in then you can use

protected void onPostExecute(String result)
    {
        textView.setText(result);
    }

但如果你想在运行后台进程更新数据,然后使用。 对于前...

But if you want to update data while running background process then use. For ex...

protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));<------
         }
         return totalSize;
     }

     protected void onProgressUpdate(Integer... progress) {  <-------
         setProgressPercent(progress[0]);
     }

有关详细请参见此链接 希望这将帮助你......!

for more detail see this link Hope this will help you...!