如何使用AsyncTask for Android显示进度对话框?

时间:2015-10-07 17:36:40

标签: android parse-platform android-asynctask progressdialog

问题是进度对话框没有显示。可能导致这种情况的原因是什么?

我的活动是onCreate:

  public class NewsActivity extends AppCompatActivity {

//Defining Variables
protected Toolbar toolbar;
protected NavigationView navigationView;
private DrawerLayout drawerLayout;
protected Button mButtonHeader;
private RecyclerView mRecyclerView;
private NewsRVCustomAdapter mAdapter;
final List<Information> data = new ArrayList<>();

private SwipeRefreshLayout mSwipeRefreshLayout;
private Context context;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news);


    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    ProgressDialog progress = new ProgressDialog(this);
    progress.setMessage("Loading...");
    new MyTask(progress).execute();

        }
  }

我的活动中的我的AsyncTask:

    //Class that handles the heavy background process of loading the list
class MyTask extends AsyncTask<Void, Void, Void> {

    public MyTask(ProgressDialog progress) {
        this.progress = progress;
    }

    @Override
    protected void onPreExecute() {

        super.onPreExecute();
        progress.setMessage("Loading...");
        progress.show();
    }

    @Override
    protected Void doInBackground(Void... arg0) {

        //This will be executed in background.

        parseQueryNewsTable();

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        super.onPostExecute(result);
        progress.dismiss();
    }
    ProgressDialog progress;
}

我的活动中包含要在AsyncTask中加载的数据的方法:

    void parseQueryNewsTable() {

    ParseQuery<ParseObject> query = ParseQuery.getQuery("news");
    query.orderByDescending("createdAt");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {

            if (e == null) {

                for (int i = 0; i < objects.size(); i++) {
                    Information information = new Information();

                    information.mNewsTitle = objects.get(i).getString("name");
                    information.mNewsStory = objects.get(i).getString("shortinfo");

                    //news table from parse
                    information.mNewsType = objects.get(i).getString("type");
                    information.mNewsInformation = objects.get(i).getString("info");

                    String[] imgUrl = new String[objects.size()];
                    //Each image is obtained from the object and their URLS and stored in a string Array
                    //This array will be passe to the adapter in a TextView containing all the
                    //URLS to be stored in another Array that holds the URLS as string and bind to the ViewHolder
                    imgUrl[i] = objects.get(i).getParseFile("image").getUrl();

                    //setting the urls to the adapter
                    information.setNewsUrls(imgUrl[i]);

                    //setting image file to the drawable
                    information.setNewsPhotoID(R.drawable.ndtc_navlogo);


                    data.add(information);
                }//End if
            } else {

                //sorry there was a problem, advice user

                Toast.makeText(getApplicationContext(), "A problem occurred. May be due to an internet or data connection", Toast.LENGTH_LONG).show();


            }//End else

            //initialize the recycler view
            RecyclerView rv = (RecyclerView)findViewById(R.id.news_recycler_view_layout);
            //initialize adapter
            NewsRVCustomAdapter mAdapter = new NewsRVCustomAdapter(getApplicationContext(), data);
            //use recycler view initialized variable to set adapter to current state
            rv.setAdapter(mAdapter);
            //notify data changes
            mAdapter.notifyDataSetChanged();


        }
    });//End Query

}

1 个答案:

答案 0 :(得分:1)

在调用findInBackground函数之前,您应该显示进度对话框,并在完成所有操作后隐藏进度条。

void parseQueryNewsTable() { 

    // Show progress here
    ParseQuery<ParseObject> query = ParseQuery.getQuery("news");
    query.orderByDescending("createdAt");
    query.findInBackground(new FindCallback<ParseObject>() {
        @Override 
        public void done(List<ParseObject> objects, ParseException e) {

            if (e == null) { 

                for (int i = 0; i < objects.size(); i++) {
                    Information information = new Information();

                    information.mNewsTitle = objects.get(i).getString("name");
                    information.mNewsStory = objects.get(i).getString("shortinfo");

                    //news table from parse 
                    information.mNewsType = objects.get(i).getString("type");
                    information.mNewsInformation = objects.get(i).getString("info");

                    String[] imgUrl = new String[objects.size()];
                    //Each image is obtained from the object and their URLS and stored in a string Array 
                    //This array will be passe to the adapter in a TextView containing all the 
                    //URLS to be stored in another Array that holds the URLS as string and bind to the ViewHolder 
                    imgUrl[i] = objects.get(i).getParseFile("image").getUrl();

                    //setting the urls to the adapter 
                    information.setNewsUrls(imgUrl[i]);

                    //setting image file to the drawable 
                    information.setNewsPhotoID(R.drawable.ndtc_navlogo);


                    data.add(information);
                }//End if 
            } else { 

                //sorry there was a problem, advice user 

                Toast.makeText(getApplicationContext(), "A problem occurred. May be due to an internet or data connection", Toast.LENGTH_LONG).show();


            }//End else 

            //initialize the recycler view 
            RecyclerView rv = (RecyclerView)findViewById(R.id.news_recycler_view_layout);
            //initialize adapter 
            NewsRVCustomAdapter mAdapter = new NewsRVCustomAdapter(getApplicationContext(), data);
            //use recycler view initialized variable to set adapter to current state 
            rv.setAdapter(mAdapter);
            //notify data changes 
            mAdapter.notifyDataSetChanged();

           // Hide progress here
        } 
    });//End Query 

}