简体   繁体   中英

Progress Dialog in GridView

My code as follows:

public class wall extends Activity {
GridView webgridview;
Button web;

ProgressDialog pd;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wall);

pd = ProgressDialog.show(wall.this,"Loading", "Please Wait...",true);
                new Thread() {
                    public void run() {
                    try {
webgridview.setAdapter(new WebImageAdapter(wall.this));
}catch(Exception e)
{
}
handler.sendEmptyMessage(0);
pd.dismiss();
}
}.start();
private Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        Toast.makeText(wall.this, "finished", 1).show();
    }
};

And my adapter class as follows. I code progress dialog in above class only.

public class WebImageAdapter extends BaseAdapter {
private Context mContext;
public static String[] myRemoteImages = {
    "http://www.evergreenhits.com/ad/wallpapers/3d_1.jpg",
    "http://www.evergreenhits.com/ad/wallpapers/3d_2.jpg",
    "http://www.evergreenhits.com/ad/wallpapers/3d_3.jpg",
    "http://www.evergreenhits.com/ad/wallpapers/3d_4.jpg",
    "http://www.evergreenhits.com/ad/wallpapers/3d_5.jpg"
        };

public WebImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    //return animals.length;
     return this.myRemoteImages.length;

}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

// create a new ImageView for each item referenced by the Adapter
public ImageView getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    imageView = new ImageView(mContext);
    if (convertView == null) {

        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        imageView.setPadding(8, 8, 8, 8);
  } else {
        imageView = (ImageView) convertView;
    }

try { 
URL aURL = new URL(myRemoteImages[position]); 
URLConnection conn = aURL.openConnection(); 
conn.connect(); 
InputStream is = conn.getInputStream(); 
BufferedInputStream bis = new BufferedInputStream(is); 
Drawable d=Drawable.createFromStream(bis, "src Name");

bis.close(); 
is.close(); 
imageView.setImageDrawable(d); 
} catch (IOException e) { 
Log.e("DEBUGTAG", "Remote Image Exception", e); 
} 

imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
imageView.setLayoutParams(new GridView.LayoutParams(150, 150)); 
    return imageView;
}

The progress Dialog doesnt work properly. It shows and suddenly gone before the images loaded in gridview. Please help me asap.

Use AsyncTask:

calling:

dialog=ProgressDialog.show(wall.this,"Loading", "Please Wait...",true);
        doback dob=new doback();
        dob.execute();

Class

class doback extends AsyncTask<URL, Integer, Long>
    {

        @Override
        protected Long doInBackground(URL... arg0) 
        {
            try
            {
                        //getImagepaths from Server
            }
            catch(Exception e)
            {

            }
            return null;
        }
        protected void onProgressUpdate(Integer... progress) 
        {

        }
        protected void onPostExecute(Long result) 
        {
            try
            {
                webgridview.setAdapter(new WebImageAdapter(wall.this));

                dialog.dismiss();
            }
            catch(Exception e)
            {
                e.printStackTrace();
                dialog.dismiss();
            }
        }
    }

Adapter Code is some like this:

class WebImageAdapter extends BaseAdapter{

//      @Override
        public int getCount() {

        return names.length;
        }

//      @Override
        public Object getItem(int position) {

        return null;
        }

//      @Override
        public long getItemId(int position) {

        return 0;
        }

//      @Override
        public View getView(int pos, View convertView, ViewGroup parent) {
        View row=getLayoutInflater().inflate(R.layout.nameitem,null);
        ImageView image=(ImageView)row.findViewById(R.id.image);

        image.setImageBitmap(convertImage(myRemoteImages[position]))// convertImage is to convert url to bitmap
        return row;
        }

        }

Edit - From the Android Developer Documentation:ProgressDialog

This class was deprecated in API level 26.

ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress.

setAdapter(), do not take more time, its getView() of Adapter class on each time hit web and taking time. So using of progressDialog on setAdapter is not appropriate. On scroll of your grids it took lot of time by using your code, as getView() call automatically on scrolling view.

you should have a reference from LazyList for loading image from web and placing them in ImageView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM