Jacek Kwiecień
Jacek Kwiecień

Reputation: 12649

ArrayAdapter loses some info on scrolling

I'm geting really confused here. I filled my ArrayAdapter once on activity creation. Its filled with simple objects. Among the object's parametrs there is one boolean which I use on getView() method of adapter. This is the concerning part:

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

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

When i scroll the list up and down several times, resultAvalible boolean starts changing (?!). No other information on the object changes, I hold couple of strings there which I display on TextViews in my list cell. List stays as it was... besides this boolean params which changes and makes my ImageView (attachment) dissapear.

My bollean param is private, there is no even a set method for it. Only way its beeing set is in constructor.

I have no clue what is happening, please need some help on that.

My custom adapter code due to requests:

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;
    }
}

Upvotes: 0

Views: 420

Answers (1)

user
user

Reputation: 87064

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);
}

Upvotes: 2

Related Questions