0

当我从 SD 卡加载图像并将它们显示在我的列表视图中时,我遇到了问题。它们最初加载得很好,但是当我开始上下滚动时,图像会全部变大,而不是与相应列表项一起使用的图像

这是我做所有事情的适配器

@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,我给出了图像将显示的视图和图像的 uri

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);
    }

}

我猜这与处理图像有关,AsyncTask但我该如何解决呢?

4

2 回答 2

1

我忘记设置默认图像了

image.setImageResource(R.drawable.ic_contact_picture);
于 2012-08-09T19:32:00.597 回答
0

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

于 2012-08-02T23:15:48.633 回答