Android add a loading view to an AysncTask

℡╲_俬逩灬. 提交于 2019-12-25 00:22:27

问题


Hi there i have an Asynctask that wroks only problem is the screen is blank until everything has loaded so i have created a loadingcell view but i'm wanting to know how to have it show the loading view until everything is loaded.

here what i have tried but it doesn't work

public class PostTask extends AsyncTask<Void, String, Boolean> {



        @Override
        protected Boolean doInBackground(Void... params) {
            boolean result = false;


            loadFixtures();
            publishProgress("progress");
            loadResultsFeed();
            publishProgress("progress");
            loadNewsFeed();
            publishProgress("progress");
            return result;
        }


        protected void onProgressUpdate(String... progress) {
            StringBuilder str = new StringBuilder();
                for (int i = 1; i < progress.length; i++) {
                    str.append(progress[i] + " ");

                  loadingView = LayoutInflater.from(getBaseContext()).inflate(R.layout.loadingcell,
                         null);

                }
        }

            @Override
        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);
            Log.v("BGThread", "begin fillin data");
             FillData();
        }
    }

回答1:


protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        Log.v("BGThread", "begin fillin data");
         FillData();
     // hide your view here                                <---------
     // for ex:    
      progressbar.setVisibility(View.GONE);                <---------

    }



回答2:


I have done this way. You can refer

 private class DownloadingProgressTask extends
        AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(ShowDescription.this);


    protected void onPreExecute() {
        this.dialog.setMessage("Please wait");
        this.dialog.show();         

    }

    protected Boolean doInBackground(final String... args) {
        try {
            downloadFile(b.getString("URL"));
            return true;
        } catch (Exception e) {
            Log.e("tag", "error", e);
            return false;
        }
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        if (success) {
            Toast.makeText(ShowDescription.this,
                    "File successfully downloaded", Toast.LENGTH_LONG)
                    .show();
            imgDownload.setVisibility(8);
        } else {
            Toast.makeText(ShowDescription.this, "Error", Toast.LENGTH_LONG)
                    .show();
        }
    }

}



回答3:


Try this.

private class BackgroundTask1 extends
        AsyncTask<String, Void, String> {

private ProgressDialog dialog;

    protected void onPreExecute() {
        /** Initialise progress dialog and show*/
        dialog = new ProgressDialog(currentActivity.this);
        dialog.setMessage("Loading.....");
        dialog.setCancelable(false);
        dialog.show();
    }


    protected String doInBackground(final String... args) {
        // do your stuff
               return anyString // as an example

    }


    protected void onPostExecute(String temp) {
        Log.v("String:::::",temp);
        dialog.dismiss();  // dismiss progress dialog
    }
}


来源:https://stackoverflow.com/questions/11013505/android-add-a-loading-view-to-an-aysnctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!