如何在Android应用程序的网格视图中加载图像时显示进度条

时间:2011-07-22 11:46:06

标签: android gridview progress-bar

在我的应用程序中,当一个活动启动时,我正在点击一个URL,并从中得到一个xml文件。 xml文件包含各种图像的URL。我正在解析这些值并将它们存储在数组列表中。从该列表中,图像已加载到网格视图中。当此过程开始时有时会在从我的Web服务加载图像时出现黑屏。所以在这种情况下我想显示一个进度条。

以下是显示进度条

的代码
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;
    }
}

但是我的应用程序崩溃了。解析函数表现良好,但到达

 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)));
    }
}   
它崩溃了。怎么做,请帮帮我

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您需要一种从AsyncTask中访问您的活动的方法 - 您不能简单地编写&#34; ProfileGridView.this&#34;因为它不是一个封闭的任务。您应该覆盖asynctask的构造函数,并将调用活动的上下文与其一起传递。