Vlad Alexeev
Vlad Alexeev

Reputation: 2204

Custom Adapter shows only positions 0-6 (that fits on screen)

Here's my adapter and for some reason in getView it shows only on-screen positions.

I use ((TextView) view.findViewById(R.id.name)).setText(items.get(position).name+" pos="+position); to log out which position is on the screen and it shows only 0-6 positions over and over and over.

It is obviously giving me numbers of recyclable on-screen positions, but I need real ListView positions like 55,100 and so on. I guess I'm missing something simple and important.

The adapter is very very generic:

public class BoxAdapter extends BaseAdapter{
     ArrayList<ItemUnit> items;
     Context context;
     LayoutInflater lInflater;

    public BoxAdapter (Context context, ArrayList<ItemUnit> items){
        this.context=context;
        this.items=items;
        lInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
Log.d("MyLog","items.size()="+items.size());
        return items.size();
    }

    @Override
    public Object getItem(int position) {

        return items.get(position);
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
      public View getView(int position, View convertView, final ViewGroup parent) {
        View view = convertView;
                        if (view == null) { 
                            view = lInflater.inflate(R.layout.listitem, parent, false);
                            ((TextView) view.findViewById(R.id.id)).setText(""+items.get(position).id);
                            ((TextView) view.findViewById(R.id.name)).setText(items.get(position).name+" pos="+position);
                            ((TextView) view.findViewById(R.id.address)).setText(items.get(position).address);
                        }
        return view;
    }

}

Upvotes: 0

Views: 145

Answers (1)

Swayam
Swayam

Reputation: 16354

You are using convertView, which basically reuses the views. So, once the views are rendered they are reused. Otherwise you are inflating it.

But you are showing the values only when you are inflating. So, when the next views are being reused there is no display.

What you should do is something like this :

Check if convertView can be reused. If not, inflate it. And in both the cases, show the display.

    public View getView(int position, View convertView, final ViewGroup parent) {
            View view = convertView;
            if (view == null) 
                   view = lInflater.inflate(R.layout.listitem, parent, false);

             ((TextView) view.findViewById(R.id.id)).setText(""+items.get(position).id);
             ((TextView) view.findViewById(R.id.name)).setText(items.get(position).name+" pos="+position);
             ((TextView) view.findViewById(R.id.address)).setText(items.get(position).address);

            return view;
        }

Upvotes: 1

Related Questions