AsyncTask - after execution, how to update view?

前端 未结 3 686
北恋
北恋 2020-12-16 19:37

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 th

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 20:18

    I am guessing the question is more about how to get hold of the UI View if the asyncTask is in a separate file .

    In that case you have to pass the context to the Async task and use that to get the view.

    class MyAsyncTask extends AsyncTask {
    
        Activity mActivity;
    
        public MyAsyncTask(Activity activity) {
           mActivity = ativity;
        }
    

    And then in your onPostExecute use

    int id = mActivity.findViewById(...);
    

    Remember you cannot update the View from "doInBackground" since its not the UI thread.

提交回复
热议问题