列表视图图像在点击时自动改变位置?

时间:2012-09-02 16:29:13

标签: android listview android-arrayadapter

我正在开发一个应用程序,其中我有一个listview,它将通过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;
    }
}

}

请告诉我这是什么问题。急切地等待你的回应。

2 个答案:

答案 0 :(得分:0)

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

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

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

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

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

希望它有所帮助,祝你好运

答案 1 :(得分:0)

如果图片网址为空,则需要添加默认图片,只需添加其他检查并添加任何占位符