我的图片没有显示在我的列表中

时间:2016-04-18 13:47:21

标签: java android retrofit picasso

我使用改造与服务器进行通信并从中获取新闻列表然后我需要在我的应用程序中显示新闻(每个新闻是2 textview和1 imageview)。 TextViews工作得很好,但我对imageview有问题。这是我的适配器的代码:

public class NewsAdapter extends BaseAdapter {
ArrayList<News> news;
public NewsAdapter(ArrayList<News> news) {
    this.news=news;
}

public void clearData() {
    // clear the data
    news.clear();
}

@Override
public int getCount() {
    return news.size();
}

@Override
public Object getItem(int position) {
    return news.get(position);
}


@Override
public long getItemId(int position) {
    return position;
}


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

    if(convertView == null)
    {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.novosti_item,parent,false);
        viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);

    }
    else{
        view = convertView;
        viewHolder= (ViewHolder) view.getTag();

    }

    News news = (News) getItem(position);

    if(news != null) {

        viewHolder.intro.setText(news.getIntro());
        viewHolder.naziv.setText(news.getSubject());
        Picasso.with(view.getContext()).load(news.getImagePath()).fit().centerCrop().into(viewHolder.slika);
    }

    return view;


}

private class ViewHolder{
    TextView naziv;
    TextView intro;
    ImageView slika;

    public ViewHolder(View view) {

        this.naziv=(TextView)view.findViewById(R.id.novosti_title);
        this.intro=(TextView)view.findViewById(R.id.novosti_desc);
        this.slika=(ImageView)view.findViewById(R.id.novosti_slika);


    }



}
}

我非常确定问题出在这一行:;

      Picasso.with(view.getContext()).load(news.getImagePath()).fit().centerCrop().into(viewHolder.slika);

2 个答案:

答案 0 :(得分:0)

更改适配器

private Context mContext;
public NewsAdapter(ArrayList<News> news, Context context) {
       this.news=news;
       this.mContext = context;
  }

Picasso.with(mContext) .load(news.getImagePath()==null ? yourPlaceHoder : news.getImagePath()) .fit() .centerCrop() .into(viewHolder.slika);

答案 1 :(得分:0)

适配器构造函数

   private Activity mActivity;
        public NewsAdapter(ArrayList < News > news, Activity mActivity) {
            this.news = news;
            this.mActivity = mActivity;
        }

        if (news.getImagePath() != null) {
            Picasso.with(mActivity).load(news.getImagePath()).centerCrop().into(viewHolder.slika);
        } else {
            Picasso.with(mActivity).load(news.getImagePath()).centerCrop().placeHolder("placeholder").into(viewHolder.slika);

        }