2017-12-11 6 views
0

私はAndroidの初心者です。非同期タスクを使用して画像を読み込み、ListViewに入力しようとしています。しかし、私の現在のコードでは、画像は正しいImageViewsに設定されず、スクロールするたびに変更されます。私は間違いを見つけることができません。前もって感謝します。 asynctaskListViewの画像はシャッフルしています

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

注用の次のクラスを使用して

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

} 

fetchBookImagesクラスのロードマップを返します。

答えて

0

あなたが直面している問題をすでに解決している既知のライブラリがありますか?

  • Glide
  • そして、あなたはhereを見つけることができますので、多くの人Picasso

    +1

    マキシム、ありがとう、私は最初からアンドロイドを学んでいます。私は第三者のライブラリを使用しない方がいいです。 – tipi

    +0

    ようこそ。あなたの問題は、アンドロイドでのリストビューの動作(ビューのメカニズムの再利用)と関係があります。この[post](https://stackoverflow.com/a/14108676/3808178)を読んでください。詳細については、私が話していることを説明しています。 –

    +0

    ありがとう、読んで! – tipi

    関連する問題