2012-08-02 115 views
0

我遇到问题时,我从我的SD卡加载图像,并显示他们在我的列表视图。他们最初加载罚款,但是当我开始向上和向下滚动的图片让所有mized起来,不具有相应的列表项加载列表视图图像滚动滚动

这里是我的适配器,我做的一切

@Override 
    public void bindView(final View view,final Context context,final Cursor cursor){ 
     final int id = cursor.getInt(0);; 
     final QuickContactBadge image = (QuickContactBadge)view.findViewById(R.id.quickContactBadge1); 
     image.setOnClickListener(new View.OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       if(!fromGame){ 
        bowlerID = id; 
        Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        startActivityForResult(i,1); 
       } 
      } 

     }); 
     String uri = cursor.getString(3); 
     if(uri != null){ 
      image.setImageResource(R.drawable.ic_contact_picture); 
      new LoadImage().execute(new Object[]{uri,image}); 
     }else{ 

     } 

     TextView first = (TextView)view.findViewById(R.id.bListTextView); 
     TextView last = (TextView)view.findViewById(R.id.bListTextView2); 
     first.setText(cursor.getString(1)); 
     last.setText(cursor.getString(2)); 
    } 

去图像我AsyncTask我给认为该图像将显示和图像

public class LoadImage extends AsyncTask<Object,Void,Object[]>{ 

    @Override 
    protected Object[] doInBackground(Object... params) { 
     String u = (String) params[0]; 

     InputStream input=null; 
     try { 
      input = getActivity().getContentResolver().openInputStream(Uri.parse(u)); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = false; 
      Bitmap og = BitmapFactory.decodeStream(input,null,options); 
      int height = options.outHeight; 
      int width = options.outWidth; 
      BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture, options); 

      int imageHeight = options.outHeight; 
      int imageWidth = options.outWidth; 
      try { 
       input.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      float scaledHeight = ((float)imageHeight)/height; 
      float scaledWidth = ((float)imageWidth)/width; 

      Matrix matrix = new Matrix(); 
      matrix.postScale(scaledWidth, scaledHeight); 

      Bitmap resize = Bitmap.createBitmap(og, 0, 0, width, height, matrix, true); 
      try { 
       input.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return new Object[] {resize,params[1]};  
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    @Override 
    public void onPostExecute(Object[] params){ 
     Bitmap resizedImage = (Bitmap)params[0]; 
     QuickContactBadge image = (QuickContactBadge) params[1]; 
     image.setImageBitmap(resizedImage); 
    } 

} 

我猜测它是与HANDELING在AsyncTask图像的URI,但我怎么解决?

回答

1

我忘了设定的默认图像回

image.setImageResource(R.drawable.ic_contact_picture); 
+0

你是什么意思? – Siddharth 2014-01-17 09:38:05

+0

downvote是不必要的,它不难理解我的意思是imageview上的默认图像将被替换 – tyczj 2014-01-17 14:45:26

+0

请阅读常见问题解答。逆转downvote,并upvoted,因为它帮助我解决了我的问题。谢谢。 – Siddharth 2014-01-18 03:55:33

0

我认为ListView回收站存在问题。当您使用ListView滚动时,ListView适配器始终仅加载显示的行(n),而向下移动时,第一个位置将被重新利用为n + 1位置。所以问题是,你开始加载图像的第一个位置,然后向下滚动,现在的第一个位置是滚动ListView中的第二个位置。
出路在于使用某种行的ID来检查加载的图像是否应放置在行中。你也可以使用像Aquery这样的ImageLoader库来为你工作。