我正在制作一个图库应用程序,当我在Adapter类的Getview中执行我的asynctask时,只有onynctask的preExecution部分获取执行,而其他人不执行。从asynctask获取图像到自定义Gridview适配器
所以,我看到的是一个进度条,但不下载后的图像...我只是活动的进度条不断滚动
这里是我的Getview方法的代码
enter public View getView(int position, View convertview, ViewGroup parent) {
// TODO Auto-generated method stub
img=new ImageView(mContext);
new DownloadImage().execute();
return img;
}
这是我Asynctaskclass
private class DownloadImage extends AsyncTask<String, Void, Bitmap>{
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(mContext);
// Set progressdialog title
mProgressDialog.setTitle("Download Image");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Bitmap doInBackground(String... URL) {
// TODO Auto-generated method stub
String imageURL = URL[0];
Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
// Set the bitmap into ImageView
img.setImageBitmap(result);
// Close progressdialog
mProgressDialog.dismiss();
}
}
我想执行onPreExecution我GetView方法返回IMG变量,以我的MainActivity后,只需右键class..here是
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setTitle("My Gallery");
GridView g=(GridView)findViewById(R.id.gallery);
// Gallery g=(Gallery)findViewById(R.id.gallery);
// ImageAdapter width=new ImageAdapter(context);
g.setAdapter(new ImageAdapter(this));
Resources r = getResources();
float padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
GRID_PADDING, r.getDisplayMetrics());
g.setNumColumns(NUM_OF_COLUMN);
g.setColumnWidth(columnWidth);
g.setStretchMode(GridView.NO_STRETCH);
g.setPadding((int) padding, (int) padding, (int) padding,
(int) padding);
// g.setHorizontalSpacing((int) padding);
// g.setVerticalSpacing((int) padding);
g.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View v, int position,
long id) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),ImageViewPager.class);
i.putExtra("id", position);
startActivity(i);
}
});
有人可以告诉我该怎么做?
所以你的意思是说,我应该在MainActivity并让所有的影像,然后应该设置适配器后,执行的AsyncTask? – 2014-08-29 14:32:32
是的。根据您的图片的数量/大小,您可能需要考虑我提到的图书馆。但是,是的,你不想在'getView()' – codeMagic 2014-08-29 14:42:15
谢谢@codeMagic。你可以参考我的任何示例代码? – 2014-08-29 18:16:40