Skip to content Skip to sidebar Skip to footer

To Use Progressdialog Till Gridview Gets Loaded From Webservice

I am fetching Image and Text for GridView from a webservice, so its takes some time to display the GridView. I want to show a ProgressDialog till Grid gets fully loaded. What I hav

Solution 1:

Try to put all rendering part from server in doInBackground() and set the adapter in onPostExecute() . And even start the progressdialog in onPreExecute() in and dismiss it on onPostExecute() but not in onCreate(). I think it will solve ur problem....

Solution 2:

This should be your inner AsyncTask class, change parameters as you need.

privateclassyourTaskextendsAsyncTask<Void, Void, ArrayList> {

    String message;
    ProgressDialog dialog;

    publicrefreshTask(String message) {
        this.message = message;
        this.dialog = newProgressDialog(PCGridMain.this);
    }

    @OverrideprotectedvoidonPreExecute() {
        dialog.setMessage(message);
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        dialog.show();  
    }

    @Overrideprotected ArrayList doInBackground(String... params) {
        // Some work
    }

    @OverrideprotectedvoidonPostExecute(ArrayList result) {
        if(dialog.isShowing())
                    dialog.dismiss();
    }
}

So you may call this class like:

newyourTask('Dialog message').execute();

I hope it solves your issue.

Solution 3:

Here I am giving the complete answer to my question, So that it may help others to make it done easily...

publicclassPCGridMainextendsActivity
{
    WebServiceweb=newWebService();
    private GridView gridView;
    ProgressDialog dialog;
    Bitmap icon;


int i, total;
URL url= null;
List<GridItem> list;

@OverridepublicvoidonCreate(Bundle grid)
{
    super.onCreate(grid);

    Log.v("GridMain", "setContent");     
    setContentView(R.layout.main);
    gridView = (GridView)findViewById(R.id.gridView1);

    DialogWorkdWork=newDialogWork();
    dWork.execute();

}



privatevoidForLoop()
{

    for(i=0; i<total; i++)
        {
                Log.v("Try Block", "See what we get:-");
                try 
                {
                    Log.v("GridMain", "try url"  + Integer.toString(i));
                    url = newURL(web.arr[i][2]);
                }
                catch (MalformedURLException e)
                {   
                    Log.v("GridMain", "catch MalformedURLException" + Integer.toString(i));
                    e.printStackTrace();
                }


                try
                {
                    Log.v("GridMain", "try BitmapFactory"  + Integer.toString(i));
                    icon = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                } 
                catch (IOException e)
                {   
                    Log.v("GridMain", "catch IOException"  + Integer.toString(i));
                    e.printStackTrace();
                }

                list.add(newGridItem(icon, web.arr[i][1]));                    // Adding Icon & LAbel              

        }    

}



privateOnItemClickListenerItemlistener=newOnItemClickListener()
{
    @OverridepublicvoidonItemClick(AdapterView<?> arg0, View view, int position, long id)
    {
        ViewHolderholder= (ViewHolder)view.getTag();

        if(holder == null)
        {
            return;
        }
        Toast.makeText(PCGridMain.this, holder.label.getText(), Toast.LENGTH_SHORT).show();
                                                                            Log.v("GridMain", "Intent Creation");
        Intentintent=newIntent(view.getContext(), ShowService.class);   Log.v("GridMain", "Intent Created");
        intent.putExtra("ServiceId", web.arr[position][0]);                 Log.v("GridMain", "ValueAdded Sid");
        intent.putExtra("SName", holder.label.getText());                   Log.v("GridMain", "ValueAdded SName");

        startActivity(intent);          

    }
};


classDialogWorkextendsAsyncTask<URL, Integer, String>
{   
    protectedvoidonPreExecute()
    {
        Log.v("GridMain", "PreExecute()");
        dialog = ProgressDialog.show(PCGridMain.this, "Loading...", "Loading App, Please wait.", false, true);

    }
    protected Long doInBackground(URL... params) 
    {
                    Stringresponse="";
        try
        {
            Log.v("GridMain", "doInBackground");
            response = web.WebService1();
            total = web.totalService; 

        }
        catch (InterruptedException e)
        {
            Log.v("GridMain", "InterruptedException");
            e.printStackTrace();
        }           
        return response;
    }       

    protectedvoidonPostExecute(String result)
    {
        try
        {
                            // Response is in RESULT_VAR
            Log.v("GridMain", "onPostExecute");
            list = newArrayList<GridItem>();
            ForLoop();
            gridView.setAdapter(newGridAdapter(PCGridMain.this, list));
            gridView.setOnItemClickListener(Itemlistener);
            dialog.dismiss();
        }
        catch (Exception e)
        {
            Log.v("GridMain", "Exception e");
            e.printStackTrace();
            dialog.dismiss();
        }
    }

}   
}

I have used it as according to my needs, its just for the help, the complete code might give problem to you. So just take it as a reference.

Thanks & Regards,

Haps.

Solution 4:

try dialog with this code else code seems working

dialog= newProgressDialog(this);
                dialog.setMessage("Loading");
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setCancelable(false);        
                dialog.show();

Post a Comment for "To Use Progressdialog Till Gridview Gets Loaded From Webservice"