我有一个来自asynchtask的代码:
Configuration configuration = getResources().getConfiguration();
int screenWidthDp = configuration.screenWidthDp;
GridView grid_foto=new GridView(context);
grid_foto.setNumColumns(3);
LinearLayout.LayoutParams parametri = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
grid_foto.setLayoutParams(parametri);
grid_foto.setAdapter(new ImageAdapter(context, album_foto, screenWidthDp));
grid_foto.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
anteprima.setVisibility(View.VISIBLE);
findViewById(R.id.loading_2).setVisibility(View.VISIBLE);
new DownloadImageTask(anteprima_img).execute(id_foto[position]);
}
});
grid_linear.addView(grid_foto);
此代码在活动开始时调用,并在用户添加图像时调用。问题是imageadapter不断增长,而不是仅在传递图像时才满。 例如:活动开始时适配器中有image_1,image_2,image_3:当用户添加照片时...而不是image_1,image_2,image_3,image_4适配器是image_1,image_2,image_3,image_1,image_2 ,image_3,image_4 !!!!
为什么这个东西?我怎么强行清理适配器?ImageAdapter:
public class ImageAdapter extends BaseAdapter implements ListAdapter {
private Context context;
int width=0;
public ImageAdapter(Context context, String[] album_foto,int larghezza) {
super();
this.context = context;
width=larghezza;
GridViewConfig.addImageUrls(album_foto);
}
@Override
public int getCount() {
return GridViewConfig.getResim_list().size();
}
@Override
public Object getItem(int position) {
return GridViewConfig.getResim_list().get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView==null){
imageView=new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(width/3,width/3));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(2,2,2,2);
}else{
imageView=(ImageView)convertView;
}
new DownloadImageTask(imageView).execute(GridViewConfig.getResim_list().get(position));
return imageView;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}