ProgressDialog on Button点击不工作的android

时间:2014-06-25 05:14:58

标签: android android-asynctask progressdialog

我有一个按钮,点击它时应显示ProgressDialog。 API调用在后台发生。

我使用AsyncTask与API对话。我还将ProgressDialog初始化放在onPreExeceute()onPostExecute()中的dismiss()中。但是,单击按钮后,ProgressDialog不会显示,但会在4-5秒后显示。

这是我的代码: -

 case R.id.action_refresh:

            try
            {
                String registerContet = myurl;
                items = new FetchItems().execute(registerContet).get();
                updatedList = new GetList().execute(items).get();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            fragment = new HomeFragment(someparams,updatedList);
            return true;


        private class FetchItems extends AsyncTask<String, String, String> 
            {
                // TODO Auto-generated method stub
                ProgressDialog myDialog; 

                        @Override
                        protected String doInBackground(String... params) 
                        {
                            // TODO Auto-generated method stub
                            //My API call
                        }





            @Override
                        protected void onPostExecute(String result) 
            {
                            // TODO Auto-generated method stub
                            super.onPostExecute(result);
                            if(refDialog!=null)
                            {
                                refDialog.dismiss();
                            }
                        }





                        @Override
                        protected void onPreExecute() 
                        {
                            // TODO Auto-generated method stub
                            super.onPreExecute();


                            if(refDialog!=null)
                            {
                                refDialog =null;
                            }
                            refDialog =  ProgressDialog.show(Home.this, "", "Please wait");
                        }




 private class GetList extends AsyncTask<String, String, List<News>>
            {


                @Override
                protected void onPostExecute(List<News> result) 
                {
                    // TODO Auto-generated method stub
                    super.onPostExecute(result);

                }

                @Override
                protected void onPreExecute() 
                {
                    // TODO Auto-generated method stub
                    super.onPreExecute();



                }

                @Override
                protected List<News> doInBackground(String... params)
                {
                    // TODO Auto-generated method stub
                    myNewsList.clear();
    //Some JSON parsing                 


                        }

                    }
                    catch (JSONException e) 
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    return myNewsList;
                }

任何人都可以帮助我吗? 感谢。

1 个答案:

答案 0 :(得分:2)

你有

 new GetList().execute(items).get();
 new FetchItems().execute(registerContet).get();

调用get()阻止ui线程等待结果。只需致电.execute(params)

您可以在doInbackground中返回结果,并根据onPostEecute()中相应的结果返回更新ui。

阅读文档

http://developer.android.com/reference/android/os/AsyncTask.html