简体   繁体   English

ListView中的图像不断改组

[英]Images in ListView keep shuffling

I'm an Android newbie, trying to load some images using an async task and populate them into a ListView . 我是一名Android新手,尝试使用异步任务加载一些图像并将其填充到ListView However with my current code, the images are not getting set to the correct ImageViews and they change whenever I scroll. 但是,使用我当前的代码,图像未设置为正确的ImageViews并且每当我滚动时它们都会更改。 I can not find the mistake I am making. 我找不到我犯的错误。 Thank you in advance. 先感谢您。

public class BookAdapter extends ArrayAdapter<Book>  {

    public BookAdapter(Context context, ArrayList<Book> objects) {
        super(context, 0, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        View booksView = convertView;

        // inflate a view if its empty
        if (booksView == null) {
            holder = new ViewHolder();

            booksView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);

            holder.bookName = booksView.findViewById(R.id.book_name_textView);
            holder.publishedDate = booksView.findViewById(R.id.dateTextView);
            holder.bookAuthor = booksView.findViewById(R.id.author_textView);
            holder.bookImage = booksView.findViewById(R.id.book_image);


            booksView.setTag(holder);

        } else {
            holder = (ViewHolder) booksView.getTag();

        }

        Book currentBook = getItem(position);

            if (holder.bookImage != null && currentBook.getBookImage()!=null) {
                new LoadImages(holder.bookImage).execute(currentBook.getBookImage());
            }

            holder.bookName.setText(currentBook.getBookName());
            holder.bookAuthor.setText(currentBook.getAuthor());
            holder.publishedDate.setText(currentBook.getDate());

        return booksView;

    }

    private class ViewHolder {
        ImageView bookImage;
        TextView bookName;
        TextView bookAuthor;
        TextView publishedDate;
    }

}

Using the following class for asynctask 将以下类用于asynctask

    public class LoadImages extends AsyncTask<String,Void,Bitmap>{
        private WeakReference<ImageView> imageview;
        public LoadImages(ImageView imv){
            imageview=new WeakReference<ImageView>(imv);
        }
        /** Background process
         * input:url
         * output: Bitmap image
         * It passed into onPostExecute method
         **/
        @Override
        protected Bitmap doInBackground(String... urls) {

        return QueryUtils.fetchBookImages(urls[0]);
        }

        /** This method called after the doINputBackground method
         * input:Bitmap image
         * output: image set into the image view
         * Image view  passed from RecyclerViewOperation to ShowImage class through constructor
         **/
        @Override
        protected void onPostExecute(Bitmap result) {
            if((imageview!=null)&&(result!=null)){
                ImageView imgview=imageview.get();


                if(imgview!=null){

                    imgview.setImageBitmap(result);
                }
            }
        }
    }

Note: fetchBookImages class loads returns bitmap. 注意: fetchBookImages类加载将返回位图。

Please there are well known libraries that already solve the problem you are facing. 请使用已经解决您所面临问题的知名库。

And so many others that you can find here . 还有很多其他您可以在这里找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM