0

我在这里真的很困惑。我在创建活动时填写了我的 ArrayAdapter。它充满了简单的物体。在对象的参数中,我在适配器的 getView() 方法中使用了一个布尔值。这是有关的部分:

MedicalExamination object = getItem(position); //object is acquired at the begining of getView()

if (!object.isResultAvalible()) 
  viewHolder.attachment.setVisibility(View.GONE);

当我上下滚动列表几次时,resultAvalible 布尔值开始变化(?!)。关于对象的其他信息没有变化,我在那里保存了几个字符串,这些字符串显示在列表单元格的 TextViews 上。列表保持原样......除了这个布尔参数会改变并使我的 ImageView(附件)消失。

我的 bolean 参数是私有的,甚至没有设置它的方法。唯一的方法是它的蜜蜂集在构造函数中。

我不知道发生了什么,请对此有所帮助。

由于请求,我的自定义适配器代码:

private class ExaminationsAdapter extends ArrayAdapter<MedicalExamination> {
    private int resource;

    public ExaminationsAdapter(Context context,
            List<MedicalExamination> objects) {
        super(context, R.layout.examinations_list_item, objects);
        resource = R.layout.examinations_list_item;
    }

    class ViewHolder {
        public TextView examinationsNames;
        public TextView date;
        public ImageView attachment;
    }

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

        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(resource, null, true);
            viewHolder = new ViewHolder();
            viewHolder.examinationsNames = (TextView) view.findViewById(R.id.examinations_item_title);
            viewHolder.date = (TextView) view.findViewById(R.id.examinations_item_date);
            viewHolder.attachment = (ImageView) view.findViewById(R.id.examinations_item_attachment);

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

        if (object == null) {
            return view;
        }

        viewHolder.examinationsNames.setText(object.getExaminationsNames());
        viewHolder.date.setText(object.getDate());

        if (!object.isResultAvalible()) 
            viewHolder.attachment.setVisibility(View.GONE);

        setOnListTouchListener(view);
        return view;
    }
}
4

1 回答 1

2

You need to take in consideration that you could have a recycled view on your hands(one for which the view may be hidden from where this row was used), so:

if (!object.isResultAvalible()) {
    viewHolder.attachment.setVisibility(View.GONE);
} else {
    // revert the image status to the default
    viewHolder.attachment.setVisibility(View.VISIBLE);
}
于 2012-11-27T10:28:56.817 回答