0

我正在开发一个 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。

4

1 回答 1

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();
    }
于 2013-05-21T13:04:36.417 回答