0

我正在开发一个应用程序,其中有一个列表视图,该列表视图通过 ArrayAdapter 填充。除了现在变得非常烦人的问题之外,所有的事情都运行良好。

我的列表视图有一个自定义布局,其中包含图像,然后是文本。问题在于图像部分。当我在填充列表后点击列表时,列表中的图像会交换那里的位置。例如,与第一个单元格关联的图像转到第三个单元格,反之亦然,依此类推。它只发生在图像上。文本保持在其位置。我不知道问题是什么。请帮我解决这个严重的问题。

以下是我的适配器代码:

 public class PListAdapter extends ArrayAdapter<Product> {

Context context;
ArrayList<Product> products;
LayoutInflater vi;
ProgressBar mSpinner;

private ImageView imageView;

public void setItems(ArrayList<Product> items) {
    this.products = items;
}

public ProductListAdapter(Context context, ArrayList<Product> productList) {
    super(context, 0, productList);
    // TODO Auto-generated constructor stub

    this.context = context;
    this.products = productList;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View v = convertView;
    final Product p = products.get(position);

    if (p != null) {

        if (v == null) {

            vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.sample_singlerow, null);
        }

        imageView = (ImageView) v.findViewById(R.id.product_image);
        TextView title = (TextView) v.findViewById(R.id.product_title);
        TextView summary = (TextView) v.findViewById(R.id.product_summary);
        TextView price = (TextView) v.findViewById(R.id.product_price);
        TextView type = (TextView) v.findViewById(R.id.product_type);
        ImageView pImage = (ImageView) v.findViewById(R.id.persons);

        if (imageView != null) {
            if (p.getImage() == null) {
                if (p.getImageURL() != null) {

                    new AsyncImageLoader(imageView, p.getImageURL());

                }
            }
        }

        if (title != null) {
            Log.i("Title: ", p.getName());
            title.setText(p.getName());
        }

        if (summary != null) {

            Log.i("Summary: ", p.getDescription());
            summary.setText(p.getDescription().substring(0, 110) + "...");
        }

        if (price != null) {
            Log.i("Price: ", p.getPrice());
            price.setText(p.getPrice());
        }

        if (type != null) {
            Log.i("Type: ", p.getType());
            type.setText(p.getType() + " Person");
        }

        if (pImage != null) {

            try {

                if (p.getType().equals("1")) {

                    pImage.setImageResource(R.drawable.one_person);

                } else if (p.getType().equals("2")) {

                    pImage.setImageResource(R.drawable.two_person);

                }

            } catch (NotFoundException e) {
                // TODO Auto-generated catch block
                pImage.setImageDrawable(null);
                e.printStackTrace();
            }
        }

    }

    return v;
}

编辑:

公共类 AsyncImageLoader { private final WeakReference imageViewReference;

public  AsyncImageLoader(ImageView imageView,String imageUrl) {
    imageViewReference = new WeakReference<ImageView>(imageView);
    String[] url={imageUrl};
    new BitmapDownloaderTask().execute(url);
}
// static int counter = 0;
// int imageNum = 0;

/**
 * This Interface in used by {@link AsyncImageLoader} to return a response
 * by after loading image
 */

public interface ImageCallback {
    public Drawable temp = null;

    /**
     * Load the Image in imageDrable, Image is loaded form imageUrl
     * 
     * @param imageDrawable
     *            Image in drawable format
     * @param imageUrl
     *            URL of image to be load
     */
    public void imageLoaded(Drawable imageDrawable, String imageUrl);
}

private String LOG_TAG;


class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
    private String url;


    @Override
    // Actual download method, run in the task thread
    protected Bitmap doInBackground(String... params) {
         // params comes from the execute() call: params[0] is the url.
         return downloadBitmap(params[0]);
    }

    @Override
    // Once the image is downloaded, associates it to the imageView
    protected void onPostExecute(Bitmap bitmap) {
        if (isCancelled()) {
            bitmap = null;
        }

        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null && !(bitmap==null)) {
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}

Bitmap downloadBitmap(String url) {
    final int IO_BUFFER_SIZE = 4 * 1024;

    // AndroidHttpClient is not allowed to be used from the main thread
    final HttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);

    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode +
                    " while retrieving bitmap from " + url);
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent();
                // return BitmapFactory.decodeStream(inputStream);
                // Bug on slow connections, fixed in future release.
                return BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (IOException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "I/O error while retrieving bitmap from " + url, e);
    } catch (IllegalStateException e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Incorrect URL: " + url);
    } catch (Exception e) {
        getRequest.abort();
        Log.w(LOG_TAG, "Error while retrieving bitmap from " + url, e);
    } finally {
        if ((client instanceof AndroidHttpClient)) {
            ((AndroidHttpClient) client).close();
        }
    }
    return null;
}
/*
 * An InputStream that skips the exact number of bytes provided, unless it reaches EOF.
 */
static class FlushedInputStream extends FilterInputStream {
    public FlushedInputStream(InputStream inputStream) {
        super(inputStream);
    }

    @Override
    public long skip(long n) throws IOException {
        long totalBytesSkipped = 0L;
        while (totalBytesSkipped < n) {
            long bytesSkipped = in.skip(n - totalBytesSkipped);
            if (bytesSkipped == 0L) {
                int b = read();
                if (b < 0) {
                    break;  // we reached EOF
                } else {
                    bytesSkipped = 1; // we read one byte
                }
            }
            totalBytesSkipped += bytesSkipped;
        }
        return totalBytesSkipped;
    }
}

}

请让我知道是什么问题。焦急等待您的回复。

4

2 回答 2

0

正如我所看到的,您在适配器中的视图重用和异步加载方面遇到了一些麻烦。为了优化性能和内存,而不是反复膨胀视图,Android 尝试在 ListView 的回收器中“缓存”列表项 - 这意味着同一视图(列表项)将被多次使用,每次用于不同的数据项。

如果是这样 - 让我们检查一下如果滚动现有列表会发生什么:某些视图将被填充,填充数据并开始为 ImageView 下载图像。在单独的线程中。

现在,如果在之前的 Image 成功加载之前,这个 View 将被重用于另一个数据项怎么办?目前,将为该图像启动一个新的 AsyncTask。这里出现了一个竞速条件,没有人知道 AsyncTask 将以什么顺序返回结果。

因此,我建议您将 AsyncTask 与 View 一起存储(例如,在 Tag 中),或者制作一些 hashmap。主要目的 - 确定位图加载何时完成 - 此图像是否用于另一个位图请求,如果是 - 跳过图像设置,因为它已过时。

这种方法将防止 imageViews 为另一个项目显示错误的图像。

希望对你有帮助,祝你好运

于 2012-09-02T16:55:58.120 回答
0

如果图像 url 为空,您需要添加默认图像,只需添加 else 检查并添加任何占位符

于 2017-05-12T09:36:16.737 回答