我在listview中的图片有问题,所以,让我来介绍一下我的问题:listview包含项目,每个项目包含图像,图像位于内存智能手机中(对于下载图像我使用picasso),以及何时我查看我的列表视图并滚动它,我看到每个项目中的图像彼此混淆......所以,我不知道如何解决这个问题。你能帮我吗 ?
让我展示我的列表适配器:
public class NewsAdapter extends BaseAdapter {
private List<News> newsList;
private LayoutInflater layoutInflater;
private Context context;
public NewsAdapter(Context context, List<News> newsList) {
this.newsList = newsList;
this.context = context;
this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null){
view = layoutInflater.inflate(R.layout.item_layout_news, parent, false);
}
News news = getNews(position);
if (news.picture != null) {
File imagePath;
imagePath = new File(context.getFilesDir()+ "/" + news.picture);
Picasso.with(context)
.load(imagePath)
.placeholder(R.drawable.loading)
.error(R.drawable.no_name_)
.into(news_photo);
}else {
}
return view;
}
@Override
public long getItemId(int position) {
return newsList.get(position).getId();
}
@Override
public Object getItem(int position) {
return newsList.get(position);
}
@Override
public int getCount() {
return newsList.size();
}
private News getNews(int position) {
return (News)getItem(position);
}
}
感谢您的关注。
更新
public class NewsAdapter extends BaseAdapter {
private List<News> newsList;
private LayoutInflater layoutInflater;
private Context context;
private static class FieldHolder {
public ImageView image;
}
public NewsAdapter(Context context, List<News> newsList) {
this.newsList = newsList;
this.context = context;
this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
FieldHolder fieldHolder = new FieldHolder();
if (view == null){
view = layoutInflater.inflate(R.layout.item_layout_news, parent, false);
final ImageView news_photo = (ImageView)view.findViewById(R.id.news_photo);
fieldHolder.image = news_photo;
view.setTag(fieldHolder);
}else {
fieldHolder =(FieldHolder) view.getTag();
}
News news = getNews(position);
File imagePath;
imagePath = new File(context.getFilesDir()+ "/" + news.picture);
Picasso.with(context) //передаем контекст приложения
.load(imagePath)
.placeholder(R.drawable.loading)
.error(R.drawable.no_name_)
.into(fieldHolder.image);
return view;
}
@Override
public long getItemId(int position) {
return newsList.get(position).getId();
}
@Override
public Object getItem(int position) {
return newsList.get(position);
}
@Override
public int getCount() {
return newsList.size();
}
private News getNews(int position) {
return (News)getItem(position);
}
}
它似乎没有错误,但我不确定我是否正确使用毕加索, 你能给我一些关于使用Picasso和listview的建议吗?
答案 0 :(得分:1)
你缺少一些getView的结构是不正确的应该。缺少部分请检查正确的getView结构
读到这个: http://www.survivingwithandroid.com/2013/02/android-listview-adapter-imageview.html
请参阅适配器的getView部分
答案 1 :(得分:1)
持有效率。 什么是news_photo? 在毕加索的.into中你应该传递你的布局。