Siva K
Siva K

Reputation: 4968

how to show progress bar while loading images in Grid view in android app

in my app when an activity is launched i am hitting an url and from that an xml file is got. The xml file contains the url of various images. I am parsing those values and storing them in an array list. From that list the images are been loaded in grid view. When this process gets started some times a black screen use to appear while loading images from my web service. So in that case i want to show a progress bar.

Following is my code to show progress bar

class Image extends AsyncTask<Void, Void, Void> 
 {       
       ProgressDialog dialog = new ProgressDialog(ProfileImages.this);

    protected void onPreExecute() 
    {       
        dialog.setMessage("Please wait...");
        dialog.setIndeterminate(true);
        dialog.show();
    }

        protected void onPostExecute(Void unused) 
    {
                    i.setImageBitmap(bm);
        dialog.dismiss();
    }

        @Override
    protected Void doInBackground(Void... params) 
    {
            parsing();
                grid.setAdapter(new ImageAdapter(this));
        return null;
    }
}

But my app i getting crashed. The parsing functions are doing good but when it reaches the

 grid.setAdapter(new ImageAdapter(this));

public View getView(final int position, View convertView,ViewGroup parent) 
    {
        ImageView i = new ImageView(this.myContext);
        if (convertView == null) 
        {
            i = new ImageView(myContext);
            i.setLayoutParams(new GridView.LayoutParams(105, 105));
            i.setScaleType(ImageView.ScaleType.CENTER_CROP);
            i.setPadding(8, 8, 8, 8);

             i.setOnClickListener(new View.OnClickListener()
             {
                 public void onClick(View view)
                 {

                     dialog = new ProgressDialog(ProfileGridView.this);
                     dialog.setMessage("Please wait...");
                     dialog.setIndeterminate(true);
                     dialog.show();
                     new Thread() 
                     {
                        public void run() 
                        {
                            try 
                            {
                                Thread.sleep(300);
                            }
                            catch (InterruptedException e) 
                            {
                                e.printStackTrace();
                            }
                            Bundle bundle = new Bundle();
                            bundle.putInt("key1",position); 
                            Appconstant.showLog("position clicked "+position);

                            Intent myIntent = new Intent(getBaseContext(),ProfileImages.class);
                            myIntent.putExtras(bundle);
                            startActivityForResult(myIntent, 0);
                            dialog.dismiss();
                        }
                     }.start();
                 }
             });
        } 
        else 
        {
            i = (ImageView) convertView;
        }

        try 
        {
            URL aURL = new URL(myRemoteImages[position]);
            Appconstant.showLog("Grid View URL " + aURL);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            Bitmap bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();

            i.setImageBitmap(bm);
        } 
        catch (IOException e) 
        {
            Appconstant.showErrorLog("DEBUGTAG Remtoe Image Exception" + e);
        }
        return i;
    }

    public float getScale(boolean focused, int offset) 
    {
        return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
    }
}   

it gets crashed. How to do this, please help me

Upvotes: 3

Views: 4179

Answers (2)

A. Vin
A. Vin

Reputation: 885

You need a way to access your activity from within the AsyncTask - you can't simply write "ProfileGridView.this" because it's not an enclosing task. You should override the constructor for your asynctask and pass the context of the calling activity in along with it.

Upvotes: 0

Siva K
Siva K

Reputation: 4968

class GridImages extends AsyncTask<Void, Void, Void> 
     {       
           ProgressDialog dialog = new ProgressDialog(ProfileGridView.this);

        protected void onPreExecute() 
        {       
            dialog.setMessage("Please wait...");
            dialog.setIndeterminate(true);
            dialog.show();
        }

        protected void onPostExecute(Void unused) 
        {
            grid.setAdapter(new ImageAdapter(ProfileGridView.this));
            dialog.dismiss();
        }

            @Override
        protected Void doInBackground(Void... params) 
        {
                parsing();      
            return null;
        }
    }

Upvotes: 1

Related Questions