简体   繁体   中英

Progress Dialog should spin till loading of gridview completed

I am working on one android app in which i want to display progress Dialog till loading of gridview completed. But my problem is progress dialog is spin for some intial time. Then it stops spinning.

Here is my code.

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);
    }

}

Please give me suggestion how i can make progress dialog spinning till gridview get loaded.

move ParseJSON function to doInBackground event

    @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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM