0

I am using a list view with two item in one row also a header item added through addHeaderView().

Initially I am using dummy data with 7 items so last item of the listview need to be invisible.

First time my data added successfully. But after scrolling on upside my first row's second item going to invisible but I do not want that.

Here is my adapter code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;

    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.home_grid_item, parent,
                false);

        holder = new ViewHolder();
        holder.imgPerformer = (CircularImageView) convertView
                .findViewById(R.id.imgPerformer);
        holder.listItemTextView = (TextView) convertView
                .findViewById(R.id.listItemTextView);
        holder.imgPerformer1 = (CircularImageView) convertView
                .findViewById(R.id.imgPerformer1);
        holder.listItemTextView1 = (TextView) convertView
                .findViewById(R.id.listItemTextView1);
        holder.performerFirstLi = (LinearLayout) convertView
                .findViewById(R.id.performerFirstLi);
        holder.performerSecondLi = (LinearLayout) convertView
                .findViewById(R.id.performerSecondLi);

        convertView.setTag(holder);

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

    index = position * 2;
    holder.imgPerformer.setImageResource(performerItems.get(index)
            .getIcon());
    holder.listItemTextView.setText(performerItems.get(index).getName());
    if (performerItems.size() > index + 1) {
        holder.imgPerformer1.setImageResource(performerItems.get(index + 1)
                .getIcon());
        holder.listItemTextView1.setText(performerItems.get(index + 1)
                .getName());
    } else {
        holder.performerSecondLi.setVisibility(View.INVISIBLE);
    }

    holder.imgPerformer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    holder.imgPerformer1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    return convertView;
}

private static class ViewHolder {
    public CircularImageView imgPerformer;
    public TextView listItemTextView;
    public CircularImageView imgPerformer1;
    public TextView listItemTextView1;
    public LinearLayout performerFirstLi;
    public LinearLayout performerSecondLi;
}

Need some help.

Matt Tester
  • 4,663
  • 4
  • 29
  • 32
Rezaul
  • 610
  • 8
  • 25

1 Answers1

1

It looks like you haven't properly taken into account the fact that views get reused, and if the value of convertView that's passed into getView() is not null, then it means the view is being reused, so you have to reset the state of it. So, as you sometimes call

holder.performerSecondLi.setVisibility(View.INVISIBLE);

the view will still be invisible when it's reused.

So, to get round this, you need to make sure it's set visible by calling

holder.performerSecondLi.setVisibility(View.VISIBLE);

in the block following

if (performerItems.size() > index + 1)
HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
  • Thank you so much for that answer.Finally i got the problem. – Rezaul Aug 10 '15 at 21:34
  • can u tell how the convertView worked. that problem was not happen alltime, sometimes it going to be ok sometimes not,sometimes last item going to fill. i just want to know how that woked. – Rezaul Aug 10 '15 at 21:42
  • 1
    Because creating new views and deallocating them takes a long time, to make scrolling a `ListView` smoother, Android only creates as many views as is necessary to show all the visible list items. When you scroll a list item off the display, the views become available for reuse, so when a new list item comes onto the screen Android will reuse the views it has already created. This means the views previously belonged to a different row in the list. In your case, it was sometimes reusing a list item where you had set the view invisible, so you had to make it visible again. – HexAndBugs Aug 10 '15 at 22:03
  • 1
    Depending how you scroll and where you change direction of scroll, Android will reuse different views, so sometimes you might see what you expect and sometimes you might see something that looks wrong if you haven't correctly reset the views, which is why you sometimes saw what you expected and sometimes didn't. This seemingly random behaviour is almost always an indication that you haven't correctly reset the state of your views when they are being reused. – HexAndBugs Aug 10 '15 at 22:10