Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 500
Siva K Avatar asked Jan 30 '26 13:01

Siva K


1 Answers

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;
        }
    }
like image 85
Siva K Avatar answered Feb 01 '26 02:02

Siva K