我有一個帶有縮略圖圖像的ListView。 ListView中的所有可見行都沒有問題。 但是對於那些低於可見行的新行,即使我試圖不將任何圖像分配給這些縮略圖ImageViews
,從第一行開始的圖像的複製順序與可見行中的順序完全相同。我在這些代碼行中設置了斷點,並在縮略圖ImageViews
處指定了圖像,沒有打斷點但仍然獲取圖像。背後的理論是什麼?我怎樣才能停止在可見光照下方的行自動分配圖像。 感謝爲什麼在ListView中自動設置縮略圖圖像
EDIT1:
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder viewHolder=new ViewHolder();
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(vi==null){
vi = inflater.inflate(R.layout.list_row, parent, false);
viewHolder.id=(TextView)vi.findViewById(R.id.title);
viewHolder.thumbnailImage=(ImageView)vi.findViewById(R.id.list_image);
viewHolder.activationStatus = (TextView)vi.findViewById(R.id.activated);
//lazy load image
BitmapWorkerTask task = new BitmapWorkerTask(viewHolder.thumbnailImage);
//if beyond visible rows, position
//becomes zero again, at that time cnt is not zero
//so task is not executed, to prevent image assignment
//for rows below the visible ones
if(position == cnt){
String id = listIDs.get(position);
task.execute(id);
cnt++;
}else{
cnt = 0;
}
//Lazy image update
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
dbHelper.open();
byte[] img_bytes = dbHelper.getImagebyIDnumber(params[0]);
bitmap = BitmapFactory.decodeByteArray(img_bytes, 0, img_bytes.length);
dbHelper.close();
return bitmap;
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
列表視圖中回收的意見。當你沒有圖像時,你必須指定沒有圖像。 – njzk2
我該怎麼做?現在,即使我分配,仍然複製可見行中的那些。 – batuman
從格式化代碼開始。現在看起來你正在一個方法中聲明一個命名類,它不能編譯。 (所以我猜想缺少一些東西) – njzk2