0

I am trying to create a ListView with a holder, and adding more items on scroll. The problem is, my adapter doesn't recognize the new items as new. I am very confused at this point. What is wrong here?

A simplified version of my adapters getView method:

public View getView(final int position, View convertView, ViewGroup parent) {
        final MyHolder holder;
        if (convertView == null) {
            LayoutInflater li = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = li.inflate(R.layout.empty_item, null);
            holder = new MyHolder();
            convertView.setTag(holder);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);

        } else {
            holder = (MyHolder) convertView.getTag();
        }

        return convertView;
}

MyHolder:

class MyHolder {

        public TextView tvTitle;

}

So first 3 items goes in the convertview==null, but rest of them does not. What might be the issue here?

Rami
  • 7,879
  • 12
  • 36
  • 66
yigitserin
  • 355
  • 1
  • 2
  • 13

1 Answers1

0

1) When you add new items to your adapter, you must call notifyDataSetChanged() to notifies the attached observers about data changes and refresh your views.

2) Your if block (convertview==null) will be executed, when there is a need to inflate a new item/layout, otherwise it will call the else block to recycle an old one.

This is how the ListView's recycling mechanism work:

Community
  • 1
  • 1
Rami
  • 7,879
  • 12
  • 36
  • 66