How to update ui from asynctask

后端 未结 3 583
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 07:05

I\'ve seen many examples of how to do this, but I can\'t figure how to implement it in my code.

I am using this code.
I have updated the url, so it will receive

3条回答
  •  悲&欢浪女
    2020-11-29 07:29

    You can use AsycTask and update list ui on task finished within onPostExecute.

        new AsyncTask() {
            /**
             * Before starting background do some work.
             * */
            @Override
            protected void onPreExecute() {
            }
    
            @Override
            protected String doInBackground(String... params) {
                // TODO fetch url data do bg process.
                return null;
            }
    
            /**
             * Update list ui after process finished.
             */
            protected void onPostExecute(String result) {
                   // NO NEED to use activity.runOnUiThread(), code execute here under UI thread. 
    
                     // Updating parsed JSON data into ListView
                     final List data = new Gson().fromJson(result);
                    // updating listview
                    ((ListActivity) activity).updateUI(data);
            }
    
        };
    }
    

    Update No need to use runOnUiThread inside onPostExecute, Because it's already called on and it's body executed under UIThread.

提交回复
热议问题