我需要在我的ListView中設置ImageView。我從網上下載圖像到我的列表。 爲此,我試圖擴展BaseAdapter並覆蓋方法。 我正在使用UrlImageViewHelper。 所以我找到了麻煩。有時候我應該展示一個形象,有時候不會。 但我的列表單元格總是顯示一個圖像。Android將viewImage設置爲ListView
class MySimpleAdapter extends BaseAdapter {
ArrayList<WallMessage> posts;
HashMap<Long, User> distinctUsers;
Activity activity;
WallMessage post;
User user;
LayoutInflater inflater=null;
public MySimpleAdapter(Activity a, ArrayList<WallMessage> posts, HashMap<Long, User> distinctUsers) {
this.activity = a;
this.distinctUsers = distinctUsers;
this.posts = posts;
for(int i = 0; i < posts.size(); i++) {
Log.d("myLogs", "attachSize = " + posts.get(i).attachments.size());
}
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return posts.size();
}
@Override
public Object getItem(int position) {
return posts.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView == null)
vi = inflater.inflate(R.layout.wall_item, null);
TextView authorTv = (TextView) vi.findViewById(R.id.author);
TextView postTv = (TextView) vi.findViewById(R.id.post);
TextView dateTv = (TextView) vi.findViewById(R.id.date);
TextView likesTv = (TextView) vi.findViewById(R.id.likes);
ImageView imagePhoto =(ImageView) vi.findViewById(R.id.imagePhoto);
ImageView imagePost = (ImageView) vi.findViewById(R.id.imagePost);
post = posts.get(position);
user = distinctUsers.get(post.from_id);
authorTv.setText(user.first_name + " " + user.last_name);
postTv.setText(post.text);
dateTv.setText(parseDate(post.date));
likesTv.setText(Long.toString(post.like_count));
UrlImageViewHelper.setUrlDrawable(imagePhoto, user.photo);
if(post.attachments != null && post.attachments.size() != 0) {
UrlImageViewHelper.setUrlDrawable(imagePost, post.attachments.get(0).photo.src);
}
return vi;
}
}
我知道我有來自其他細胞加載post.attachments.size()== 0 但我的列表中的所有同一節目的圖像元素。
的Android適配器將重用的意見,你檢查,你清除imagePhoto&imagePost(通過調用ImageView.setImageBitmap(空)或類似的東西)的情況下,當沒有任何圖像? – panini
謝謝,imagePost.setImageBitmap(null)正常工作。 – Anatol