2013-08-21 51 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