0

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 have done till now is as below:

public class PCGridMain extends Activity
{
    WebService web = new WebService();
    private GridView gridView;
    ProgressDialog dialog;
    Bitmap icon;


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

    @Override
    public void onCreate(Bundle grid)
    {
        super.onCreate(grid);
        dialog = ProgressDialog.show(PCGridMain.this, "Loading...", "Loading App, Please wait.", true);
        DialogWork dWork = new DialogWork();
        dWork.execute();

        setContentView(R.layout.main);
        gridView = (GridView)findViewById(R.id.gridView1);

        web.WebService1();       
        total = web.totalService;        



        list = new ArrayList<GridItem>();

        for(i=0; i<total; i++)
            {
                    Log.v("Try Block", "See what we get:-");
                    try 
                    {
                        Log.v("GridMain", "try url"  + Integer.toString(i));
                        url = new URL(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(new GridItem(icon, web.arr[i][1]));                    // Adding Icon & LAbel              

            }

        gridView.setAdapter(new GridAdapter(this, list));
        gridView.setOnItemClickListener(Itemlistener);
    }



    private OnItemClickListener Itemlistener = new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
        {
            ViewHolder holder = (ViewHolder)view.getTag();

            if(holder == null)
            {
                return;
            }
            Toast.makeText(PCGridMain.this, holder.label.getText(), Toast.LENGTH_SHORT).show();
                                                                                Log.v("GridMain", "Intent Creation");
            Intent intent = new Intent(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);          

        }
    };

    class DialogWork extends AsyncTask<URL, Integer, Long>
    {       
        protected Long doInBackground(URL... params) 
        {
            try
            {

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }

        protected void onProgressUpdate(Integer... progress)
        {

        }

        protected void onPostExecute(Long result)
        {
            try
            {
                //setContentView(R.layout.main);
                //gridView.setAdapter(new GridAdapter(PCGridMain.this, list));              
                dialog.dismiss();                   
            }
            catch (Exception e)
            {
                e.printStackTrace();
                dialog.dismiss();
            }
        }

    }

Please tell me that what code has to be placed at what exact location, whenever I do some changes, it either shows no effect or App closes due to some issue.

Thanks,

Haps.

Harpreet
  • 2,990
  • 3
  • 38
  • 52
  • You should show `ProgressDialog` in AsyncTask. Implement `onPreExecute` method and initialize dialog before task started. – Ogulcan Orhan Feb 23 '12 at 13:50
  • From app user perspective, block the whole GridView by ProgressDialog is a very bad design. User should have the ability to view/click parts of GridView while other parts is still loading. Consider using AsyncTask.onProgressUpdate() to show the loaded image on demand, instead of blocking the whole GridView and ultimately show it once all images are loaded. – yorkw Feb 23 '12 at 21:57

4 Answers4

5

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....

sandy goria
  • 144
  • 5
4

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

private class yourTask extends AsyncTask<Void, Void, ArrayList> {

    String message;
    ProgressDialog dialog;

    public refreshTask(String message) {
        this.message = message;
        this.dialog = new ProgressDialog(PCGridMain.this);
    }

    @Override
    protected void onPreExecute() {
        dialog.setMessage(message);
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        dialog.show();  
    }

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

    @Override
    protected void onPostExecute(ArrayList result) {
        if(dialog.isShowing())
                    dialog.dismiss();
    }
}

So you may call this class like:

new yourTask('Dialog message').execute();

I hope it solves your issue.

Ogulcan Orhan
  • 5,170
  • 6
  • 33
  • 49
  • Sorry bro, got FATAL EXCEPTION:MAIN. – Harpreet Feb 28 '12 at 06:16
  • The code mite have some minimal problems. It was justified by other answer, but your answer is also good, so I up-vote it. Rest You can see the complete answer which I have posted. – Harpreet Feb 28 '12 at 12:04
3

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

public class PCGridMain extends Activity
{
    WebService web = new WebService();
    private GridView gridView;
    ProgressDialog dialog;
    Bitmap icon;


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

@Override
public void onCreate(Bundle grid)
{
    super.onCreate(grid);

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

    DialogWork dWork = new DialogWork();
    dWork.execute();

}



private void ForLoop()
{

    for(i=0; i<total; i++)
        {
                Log.v("Try Block", "See what we get:-");
                try 
                {
                    Log.v("GridMain", "try url"  + Integer.toString(i));
                    url = new URL(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(new GridItem(icon, web.arr[i][1]));                    // Adding Icon & LAbel              

        }    

}



private OnItemClickListener Itemlistener = new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int position, long id)
    {
        ViewHolder holder = (ViewHolder)view.getTag();

        if(holder == null)
        {
            return;
        }
        Toast.makeText(PCGridMain.this, holder.label.getText(), Toast.LENGTH_SHORT).show();
                                                                            Log.v("GridMain", "Intent Creation");
        Intent intent = new Intent(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);          

    }
};


class DialogWork extends AsyncTask<URL, Integer, String>
{   
    protected void onPreExecute()
    {
        Log.v("GridMain", "PreExecute()");
        dialog = ProgressDialog.show(PCGridMain.this, "Loading...", "Loading App, Please wait.", false, true);

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

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

    protected void onPostExecute(String result)
    {
        try
        {
                            // Response is in RESULT_VAR
            Log.v("GridMain", "onPostExecute");
            list = new ArrayList<GridItem>();
            ForLoop();
            gridView.setAdapter(new GridAdapter(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.

Harpreet
  • 2,990
  • 3
  • 38
  • 52
2

try dialog with this code else code seems working

dialog= new ProgressDialog(this);
                dialog.setMessage("Loading");
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setCancelable(false);        
                dialog.show();
vipin
  • 2,851
  • 4
  • 18
  • 32