我正在使用listview作为列表项
我正在使用扩展ArrayAdapter
并使用ViewHolder
的自定义适配器,并且在自定义适配器中,如果存在附件,我会检查条件。如果为true,我会ImageView
可见。一切正常,但滚动时ImageView
消失了。
public class NoticeListAdapter extends ArrayAdapter<String>{
private final Activity context;
private List<String> img_url = new ArrayList<String>();
private List<String> heading = new ArrayList<String>();
private List<Long> createdDate = new ArrayList<Long>();
private List<String> documentCategoryId = new ArrayList<String>();
Date date = new Date();
public NoticeListAdapter(Activity context, List<String> img_url, List<String> heading, List<Long> createdDate, List<String> documentCategoryId) {
super(context, R.layout.attendance_list_item, heading);
// TODO Auto-generated constructor stub
this.context=context;
this.img_url = img_url;
this.heading = heading;
this.createdDate = createdDate;
this.documentCategoryId = documentCategoryId;
}
public static class ViewHolder {
ImageView profilePicture;
ImageView attachmentIcon;
TextView txtemployeeName;
TextView txtbulletinDate;
}
@Override
public View getView(final int position, View view, ViewGroup parent){
ViewHolder viewHolder;
if(view == null){
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.notice_list_item, parent, false);
viewHolder.profilePicture = (ImageView) view.findViewById(R.id.profilePic);
viewHolder.txtemployeeName = (TextView) view.findViewById(R.id.employeeName);
viewHolder.txtbulletinDate = (TextView) view.findViewById(R.id.bulletinDate);
viewHolder.attachmentIcon = (ImageView) view.findViewById(R.id.attachmentIcon);
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)view.getTag();
}
String description = heading.get(position);
description = description.length()>40?description.substring(0,40)+"...":description;
SharedPreferences pref = getContext().getSharedPreferences("authDetails", Context.MODE_PRIVATE);
ImageLoader imageLoader = new ImageLoader(getContext());
imageLoader.DisplayImage(pref.getString("base_url", "")+img_url.get(position), viewHolder.profilePicture);
viewHolder.txtemployeeName.setText(description);
viewHolder.txtbulletinDate.setText(DateUtils.getRelativeTimeSpanString(createdDate.get(position), date.getTime(), DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE));
if(documentCategoryId.get(position).equalsIgnoreCase("0"))
viewHolder.attachmentIcon.setVisibility(View.GONE);
return view;
}
}
答案 0 :(得分:2)
只需更改您的密码:
if(documentCategoryId.get(position).equalsIgnoreCase("0"))
viewHolder.attachmentIcon.setVisibility(View.GONE);
是:
if(documentCategoryId.get(position).equalsIgnoreCase("0"))
viewHolder.attachmentIcon.setVisibility(View.GONE);
else
viewHolder.attachmentIcon.setVisibility(View.VISIBLE);
答案 1 :(得分:0)
此问题来自您的自定义ImageLoader
实施。
粘贴你的代码,有一些错误没有显示图像。
或者,您可以从URL
下载图片,并将其设置为profilePicture
ImageView的来源,如下所示:
URL url = new URL(img_url.get(position));
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
viewHolder.profilePicture.setImageBitmap(bmp);
答案 2 :(得分:0)
您的代码中存在ViewHolder实现问题。您不将持有者结果设置为非空视图。而且,不要检索最近创建的持有者。
尝试改变:
view.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)view.getTag();
}
到这个
view.setTag(viewHolder);
}
viewHolder = (ViewHolder)view.getTag();
和return view;
之前:
view.setTag(viewHolder);
试试这个,固定代码:
@Override
public View getView(final int position, View view, ViewGroup parent){
ViewHolder viewHolder;
if(view == null){
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
view = inflater.inflate(R.layout.notice_list_item, parent, false);
viewHolder.profilePicture = (ImageView) view.findViewById(R.id.profilePic);
viewHolder.txtemployeeName = (TextView) view.findViewById(R.id.employeeName);
viewHolder.txtbulletinDate = (TextView) view.findViewById(R.id.bulletinDate);
viewHolder.attachmentIcon = (ImageView) view.findViewById(R.id.attachmentIcon);
view.setTag(viewHolder);
}
viewHolder = (ViewHolder)view.getTag();
String description = heading.get(position);
description = description.length()>40?description.substring(0,40)+"...":description;
SharedPreferences pref = getContext().getSharedPreferences("authDetails", Context.MODE_PRIVATE);
ImageLoader imageLoader = new ImageLoader(getContext());
imageLoader.DisplayImage(pref.getString("base_url", "")+img_url.get(position), viewHolder.profilePicture);
viewHolder.txtemployeeName.setText(description);
viewHolder.txtbulletinDate.setText(DateUtils.getRelativeTimeSpanString(createdDate.get(position), date.getTime(), DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE));
if(documentCategoryId.get(position).equalsIgnoreCase("0"))
viewHolder.attachmentIcon.setVisibility(View.GONE);
view.setTag(viewHolder);
return view;
}