2015-07-14 6 views
13

Это мой getView().Picasso + convertView: что я делаю неправильно здесь?

Я, очевидно, делаю что-то неправильно здесь, потому что в ПЕРВОМ элементе моего списка всегда нет изображения.

Проблема здесь the convertview потому что, если я не перерабатываю ее, нет проблем.

Пожалуйста, что я делаю неправильно?

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView==null) //IF I DELETE THIS IF EVERYTHING OK!!! 
    convertView = inflater.inflate(R.layout.square, null); 
    ImageView image = (ImageView) convertView.findViewById(R.id.background_image); 
    if (data.get(position).isFollowed()) { 
     int approprieteimage = getApproppreiateImage(data.get(position)); 
     Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).transform(new TealTransformation(context)).fit().into(image); 
    } else { 
     int approprieteimage = getApproppreiateImage(data.get(position)); 
     Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).fit().into(image); 

    } 
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(width/2, width/2); 
    convertView.setLayoutParams(layoutParams); 
    return convertView; 
} 
+0

ли вы проверить еще раз, но удалить '.centerCrop()' и '.Поставить()' из вашей команды Пикассо. Я считаю, что это связано с тем, что Пикассо нуждается в первом макете для выполнения этих действий. – Budius

+0

Также убедитесь, что approppreateimage имеет правильное значение для первого элемента –

+0

@Budius благодарит Budius, я пробовал, как вы предложили, не удался :-( –

ответ

1

Почему бы вам не использовать шаблон держателя в вашем адаптере?

if (convertView == null) { 
    convertView = inflater.inflate(R.layout.square, null, false); 
    holder = new Holder(convertView); 
    convertView.setTag(holder); 
} 
else { 
    holder = (Holder) convertView.getTag(); 
} 

Создайте свой держатель, как это (замените его с видом)

public static class Holder { 
    private View row; 
    private TextView title; 

    public Holder(View row) { 
     this.row = row; 
    } 

    public TextView getTitle() { 
     if (title == null) { 
      title = (TextView) row.findViewById(R.id.title); 
     } 
     return title; 
    } 
} 

Вы должны быть в порядке.

5

Привет Лиза Энн, пожалуйста, попробуйте этот

ViewHolder h; //declare viewholder global 

Ваше мнение получить с viewholder

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

     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.square, parent, false); 
      h = new ViewHolder(); 
      h.image = (ImageView) convertView.findViewById(R.id.background_image); 
      convertView.setTag(h); 
     }else{ 
      h = (ViewHolder)convertView.getTag(); 
     } 
     //display in log and check if the the url of image is here and live. 
     Log.e("checking","image file name"+data.get(position)) 
    if (data.get(position).isFollowed()) { 
     int approprieteimage = getApproppreiateImage(data.get(position)); 
     Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).transform(new TealTransformation(context)).fit().into(h.image); 
    } else { 
     int approprieteimage = getApproppreiateImage(data.get(position)); 
     Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).fit().into(h.image); 

    } 
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(width/2, width/2); 
    convertView.setLayoutParams(layoutParams); 

    return convertView; 
} 

Viewholder класса

static class ViewHolder { 
    ImageView image; 
    } 

Ваш код может позвонить findViewById() часто при прокрутке ListView, что может замедлить производительность. Даже когда адаптер возвращает завышенный вид на переработку, вам все равно нужно искать элементы и обновлять их. Способом повторного использования findViewById() является использование шаблона дизайна «view holder».

Объект ViewHolder хранит каждый вид компонентов внутри поля тега макета, поэтому вы можете сразу же получить к ним доступ, не пересматривая их повторно.

View holder - очень полезная техника, особенно при отображении изображений. Android Developer documentation

после этого, пожалуйста, покажите мне свою ошибку в журнале.

+0

Зачем это повлиять только на изображение первого элемента? Техника хорошая, но здесь нет темы. – Shakti

0

convertView = inflater.inflate (R.layout.square, parent, false);

Провести родительский контроль в ваш convertView.

0
try this one: 
    It is working smoothly for me: 

    @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      ViewHolder holder; 
      if (convertView == null) { 
       convertView = mInflater.inflate(R.layout.row_events, null); 
       holder = new ViewHolder(); 
       holder.txtdName = (TextView) convertView 
         .findViewById(R.id.txtTitle); 
       holder.txtdate = (TextView) convertView.findViewById(R.id.txtDate); 

       holder.txtSubTitle = (TextView) convertView 
         .findViewById(R.id.txtSubtitle); 
       holder.imgEvent = (ImageView) convertView 
         .findViewById(R.id.imgEvents); 
       holder.imgArrow = (ImageView) convertView 
         .findViewById(R.id.imgArrow); 
       convertView.setTag(holder); 
      } else { 
       holder = (ViewHolder) convertView.getTag(); 
      } 

      try { 
       holder.txtdName.setText(listd.get(position).getName()); 
       holder.txtdate.setText(Constants.dateFormat(listd.get(position) 
         .getStart_date())); 
       holder.txtSubTitle.setText(listd.get(position).getDescription()); 
       holder.txtSubTitle.setTextColor(Color.GRAY); 
       String imgUrl = listd.get(position).getImageUrl(); 
       // imageLoader.displayImage(imgUrl, holder.imgEvent, options); 
       Picasso.with(context).load(imgUrl).into(holder.imgEvent); 

return convertView; 
1

Как я нашел в этой ссылке: picasso issue Когда рециркуляционный вид, вы должны позвонить cancelRequest до того нагрузки нового запроса. Таким образом, код должен быть:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView==null) //IF I DELETE THIS IF EVERYTHING OK!!! 
    convertView = inflater.inflate(R.layout.square, null); 
    ImageView image = (ImageView) convertView.findViewById(R.id.background_image); 
    Picasso.with(context).cancelRequest(image); // cancel old request 
    if (data.get(position).isFollowed()) { 
     int approprieteimage = getApproppreiateImage(data.get(position)); 
     Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).transform(new TealTransformation(context)).fit().into(image); 
    } else { 
     int approprieteimage = getApproppreiateImage(data.get(position)); 
     Picasso.with(context).load(approprieteimage).centerCrop().error(R.drawable.no_image_available).fit().into(image); 

    } 
    AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(width/2, width/2); 
    convertView.setLayoutParams(layoutParams); 
    return convertView; 
} 
-1

Попробуйте

convertView = inflater.inflate(R.layout.square, parent, false); 
Смежные вопросы