进度对话框应旋转直到gridview加载完成

时间:2013-05-21 12:53:27

标签: android android-asynctask progressdialog spinning

我正在开发一个Android应用程序,其中我想显示进度对话框,直到加载gridview完成。但我的问题是进度对话在一些初始时间内旋转。然后它停止旋转。

这是我的代码。

public class allsites extends Activity {

    private final String url_select = "http://api.stackexchange.com/2.1/sites?filter=!RGB_Y51.*-(YX";
    private GridView gview;
    private ListViewCustomAdapter adapter;
    private ArrayList<Object> itemList = new ArrayList<Object>();
    private ItemBean bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.allsites);
        //GridView gridview = (GridView) findViewById(R.id.gvAllSites);
        gview = (GridView) findViewById(R.id.gvallsites);
        new task().execute();


    }

    private class task extends AsyncTask<Void, Void, GZIPInputStream> {

        private ProgressDialog progress;


        @Override
        protected void onPreExecute() {

            progress = ProgressDialog.show(allsites.this, "Loading", "Please Wait...");
        }

        @Override
        protected GZIPInputStream doInBackground(Void... params) {

            ServerData httpclient = new ServerData();
            GZIPInputStream zis = httpclient.GetServerData(url_select);
            return zis;
        }

        @Override
        protected void onPostExecute(GZIPInputStream zis) {


            ParseJSON(zis);

            if(progress!=null && progress.isShowing()==true)
                progress.dismiss();



        }
    }

    private void ParseJSON(GZIPInputStream zis)
    {
        Gson gson = new Gson();

        Reader reader = new InputStreamReader(zis);

        Sites response = gson.fromJson(reader, Sites.class);

        List<Items> items = response.getItems();

        for (Items site : items) {
            //Toast.makeText(allsites.this, site.getApi_site_parameter().toString(), Toast.LENGTH_SHORT).show();
             AddObjectToList(site.getIcon_url(),site.getName());
        }

        adapter = new ListViewCustomAdapter(this, itemList);
        gview.setAdapter(adapter);
    }

    public void AddObjectToList(String imageURL, String title)
    {
        bean = new ItemBean();

        try {
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageURL).getContent());
            bean.setImage(bitmap);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bean.setTitle(title);
        itemList.add(bean);
    }

}

请给我一些建议如何让进度对话旋转直到gridview加载。

1 个答案:

答案 0 :(得分:0)

将ParseJSON函数移动到doInBackground事件

    @Override
    protected Boolean doInBackground(Void... params) {

        ServerData httpclient = new ServerData();
        GZIPInputStream zis = httpclient.GetServerData(url_select);

        ParseJSON(zis);
        return true;
    }

    @Override
    protected void onPostExecute(Boolean zis) {

            progress.dismiss();
    }