我正在开发一个应用程序,其中有一个列表视图,该列表视图通过 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;
}
}
}
请让我知道是什么问题。焦急等待您的回复。