2017-01-01 43 views
0

我在填寫ImageView時遇到了問題ListView
問題是,所有的圖像被填充在相同的地方(在最底部的列表項的圖像視圖)。我可以看到圖像在被填滿時快速加載。所有圖像都被加載到ListView中的相同位置

public class TheaterListAdapter extends ArrayAdapter<ConstantsTheaterList> implements IResult { 

    private ViewHolder viewHolder; 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 


     if (convertView==null){ 
      convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_theater_list_item, parent, false); 

      viewHolder = new ViewHolder(); 
      viewHolder.imageView = (ImageView) convertView.findViewById(R.id.theater_list_item_image); 
      viewHolder.textViewLatitude = (TextView) convertView.findViewById(R.id.theater_list_item_latitude); 
      viewHolder.textViewLongitude = (TextView) convertView.findViewById(R.id.theater_list_item_longitude); 
      viewHolder.textViewVicinity = (TextView) convertView.findViewById(R.id.theater_list_item_vicinity); 
      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 

     //Picasso.with(getContext()).load(uri.toString()).into(mImageView); [WORKS FINE] 

     // Using Volley to fetch image from the web. 
     new VolleyService(mContext, this).volleyImageRequest(uri.toString(), 100, 100); 


     String latitude = getItem(position).latitude; 
     String longitude = getItem(position).longitude; 
     String vicinity = getItem(position).vicinity; 

     viewHolder.textViewLatitude.setText(latitude); 
     viewHolder.textViewLongitude.setText(longitude); 
     viewHolder.textViewVicinity.setText(vicinity); 

     return convertView; 
    } 

    //These are the callbacks that I receive in my Volley implementation. Now, I fill the bitmap once they are available. 
    @Override 
    public void notifySuccess(Object response) { 
     Bitmap bitmap = (Bitmap) response; 
     viewHolder.imageView.setImageDrawable(bitmap); 
    } 

    @Override 
    public void nofifyError(Object volleyError) { 
     Log.e(TAG, (String)volleyError); 
    } 


    private static class ViewHolder{ 
     ImageView imageView; 
     TextView textViewLatitude; 
     TextView textViewLongitude; 
     TextView textViewVicinity; 
    } 


} 

我相信被回收這一定是因爲意見。

但是,當我使用畢加索時,一切正常。

我該如何解決這個問題?

+0

「問題是,所有的圖像被填充在同一個地方」 - 這是因爲你沒有將圖像填充到正確的'ViewHolder'中。不是使用與請求相關聯的'ViewHolder',而是使用'viewHolder'字段中的任何'ViewHolder',它將是您最近遇到的任何'ViewHolder'。您需要更好地將Volley請求綁定到相關的「ImageView」。就我個人而言,我只是使用畢加索。 – CommonsWare

+1

@CommonsWare請詳細回答先生。我沒有得到正確的。 –

+0

@SumitJha在getView方法中,您正在使用凌空異步加載圖像。在齊射加載圖像時,視圖持有者指向的位置被改變。 要解決這個問題,您可以使用圖像加載庫,使用recyclerview或創建排球服務時使用匿名類。 –

回答

2

您面對此問題的原因是,您沒有正確使用查看持有人
您會發現,查看持有者已將其實例與每個單元格相關聯。 在這裏,當你調用「凌空」的方法,以下載PIC,然後通過它的回調方法,你填充ImageView的,使用「ViewHolders」實例,該實例以某種方式使用ListView的最後一個單元格關聯。

還有一個問題,你沒有通過你正在下載圖片的單元格的實例。實例錯位。

您可以使用畢加索爲此或UniversalImageLoader。 由於這些允許您傳遞您正在下載圖像的特定單元的imageview的實例。
實施例:

Picasso.with(本).load( 「圖像URL HERE」)代入(ViewHolder.imageView);
就是這樣。你的好去...

+0

感謝隊友的幫助:) –

0

對於每個人都在那裏,誰還是想明白爲什麼這不起作用,想解決這樣的說法,這裏是如何做到這一點:

每次你調用你的方法在後臺獲取你的圖像,你也需要在應該放置圖像的地方傳遞imageView的實例。

以下是我使用AsyncTask獲取圖像的示例。

public class TheaterListAdapter extends ArrayAdapter<ConstantsTheaterList> 

    private ViewHolder viewHolder; 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 


     if (convertView == null) { 
      convertView = LayoutInflater.from(mContext).inflate(R.layout.fragment_theater_list_item, parent, false); 

      viewHolder = new ViewHolder(); 
      viewHolder.imageView = (ImageView) convertView.findViewById(R.id.theater_list_item_image); 
      viewHolder.textViewLatitude = (TextView) convertView.findViewById(R.id.theater_list_item_latitude); 
      viewHolder.textViewLongitude = (TextView) convertView.findViewById(R.id.theater_list_item_longitude); 
      viewHolder.textViewVicinity = (TextView) convertView.findViewById(R.id.theater_list_item_vicinity); 
      convertView.setTag(viewHolder); 
     } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 


     String key = mContext.getResources().getString(R.string.google_maps_key); 
     String photoReference = getItem(position).photo_reference; 
     String maxWidth = getItem(position).max_width; 
     String maxHeight = getItem(position).max_height; 
     String url = "https://maps.googleapis.com/maps/api/place/photo?"; 

     Uri uri = Uri.parse(url).buildUpon() 
       .appendQueryParameter("key", key) 
       .appendQueryParameter("maxwidth", maxWidth) 
       .appendQueryParameter("maxheight", maxHeight) 
       .appendQueryParameter("photoreference", photoReference) 
       .build(); 

     //Picasso.with(getContext()).load(uri.toString()).into(mImageView); 

     //new VolleyService(mContext, this).volleyImageRequest(uri.toString(), 100, 100); 

     // Preparing input for MyAsyncTask... 
     Object[] object = new Object[2]; 
     object[0] = uri.toString(); 
     object[1] = viewHolder.imageView; // This is **important** 
              //Pass the instance of imageview where the image is supposed to be filled. 

     //Calling AsyncTask... 
     MyAsyncTask myAsyncTask = new MyAsyncTask(); 
     myAsyncTask.execute(object); 


     String latitude = getItem(position).latitude; 
     String longitude = getItem(position).longitude; 
     String vicinity = getItem(position).vicinity; 

     viewHolder.textViewLatitude.setText(latitude); 
     viewHolder.textViewLongitude.setText(longitude); 
     viewHolder.textViewVicinity.setText(vicinity); 

     return convertView; 
    } 
    private static class ViewHolder { 
     ImageView imageView; 
     TextView textViewLatitude; 
     TextView textViewLongitude; 
     TextView textViewVicinity; 
    } 

    private static class MyAsyncTask extends AsyncTask<Object, Void, Bitmap>{ 

     private String url; 
     private ImageView imageView; 
     @Override 
     protected Bitmap doInBackground(Object... params) { 

      url = (String) params[0]; 
      Log.v(TAG, url); 
      imageView = (ImageView) params[1]; 

      return Utility.ImageHttp.read(url); // my method to fetch the image and return the Bitmap. 
     } 

     @Override 
     protected void onPostExecute(Bitmap bitmap) { 
      imageView.setImageBitmap(bitmap); 
     } 
    } 
} 

現在你已經明白了,你可以使用畢加索這個東西。但重要的是理解它爲什麼不起作用。

相關問題