2013-08-21 56 views
-1

我有一個帶有縮略圖圖像的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); 
       } 
      } 
     } 


    } 
+0

列表視圖中回收的意見。當你沒有圖像時,你必須指定沒有圖像。 – njzk2

+0

我該怎麼做?現在,即使我分配,仍然複製可見行中的那些。 – batuman

+0

從格式化代碼開始。現在看起來你正在一個方法中聲明一個命名類,它不能編譯。 (所以我猜想缺少一些東西) – njzk2

回答

0

這是因爲ListView控件重用的觀點可見的看法越來越不可見。如果要隱藏圖像,則需要在適配器的getView()方法中明確執行此操作。

+0

在getView()中,我不想爲新行設置任何圖像,除非我故意設置它。但靜止圖像被設置爲與可見行中的圖像完全相同的順序。 – batuman

+0

即使我試圖分配圖像,也無法分配,仍然從可見行中複製圖像。 – batuman

+0

發佈您的代碼,請 –

0

檢查您的佈局。也許你默認爲你的圖片安裝了android:src =「@dr​​awable ...」。

在你的adaper中配置你的listView數據。

EDITED 22.08。

嘗試使用此:

if(vi==null){ 
    ... 
    //findView by ID here; 
    ... 
    vi.setTag(viewHolder); 
} else { 
    viewHolder = (ViewHolder) vi.getTag(); 
} 
//to do what you want here; 
//setting values etc. 

你可以找到很好的解釋 '如何與ViewHolder工作' 這裏ViewHolder Pattern

+0

是的在設置任何圖像之前,我有一個要分配的默認圖像。但現在,可見行中的圖像被完全按照相同的順序複製。 – batuman

+0

我更新了答案。覈實。 – Volodymyr