2017-01-01 63 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); 
     } 
    } 
} 

现在你已经明白了,你可以使用毕加索这个东西。但重要的是理解它为什么不起作用。