GridView 中的进度对话框

发布于 2024-12-29 14:38:50 字数 2577 浏览 0 评论 0原文

我的代码如下:

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

我的适配器类如下。我仅在上面的类中编写进度对话框。

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

进度对话框无法正常工作。它在 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南烟 2025-01-05 14:38:50

使用 AsyncTask:

调用:

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

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

适配器代码如下:

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

编辑 - 来自 Android 开发人员文档:进度对话框

此类在 API 级别 26 中已弃用。

ProgressDialog 是一个模式
对话框,阻止用户与应用程序交互。反而
使用此类时,您应该使用类似的进度指示器
ProgressBar,可以嵌入到应用的 UI 中。或者,
您可以使用通知来通知用户任务的进展。

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.

幸福还没到 2025-01-05 14:38:50

setAdapter(),不用多花时间,它的Adapter类的getView()每次访问web都花时间。所以在setAdapter上使用progressDialog是不合适的。在网格滚动时,使用代码花费了大量时间,因为 getView() 在滚动视图上自动调用。

您应该有来自 LazyList 的参考,用于从网络加载图像并将其放置在 ImageView 中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文