Search code examples
androidcustom-adaptergetview

getView of custom adapter returns wrong Position


I have written a custom adapter for a gridview. Each item in grid view has other childs views. One of them is a textView, which has implemented a click listener.

When the user clicks on the TextView, I show a Toast which shows the position of the item in gridView.

Now coming to the problem:

I assign the "position" returned by getView() as a tag to the TextView every time the getView() is called. When I scroll the "position" provided by getView() is the correct value but as soon as the scrolling stops, the position provided by getView() becomes 0, which shouldn't be zero in the first place unless I am at the top of the grid view. However the value saved in the TextView tag is always the correct value. Why is that?

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;

        if(convertView == null){
            convertView = mLayoutIflater.inflate(R.layout.grid_item, null);
            viewHolder = new ViewHolder();

            …..
            viewHolder.playButton.setOnClickListener(new View.OnClickListener() {
                 @Override
                    public void onClick(View v) {
                         Log.e(“test”, "myPos: "+ (Integer) viewHolder.mTextView.getTag()); // always returns correct position value

                        }
                    });

            convertView.setTag(viewHolder);
        }
        else
            viewHolder = (ViewHolder) convertView.getTag();


        Log.e(“test”, "GETVIEW: "+ position); // position is zero when scrolling stops & correct value when scrolling
        viewHolder.mTextView.setTag(position);

        return convertView;
    }

Best Regards


Solution

  • An AdapterView can call getView() on its Adapter quite seemingly arbitrarily. There is no guaranteed order or timing for doing so. It's irrelevant that the last position it calls the method for might be the 0 position when scrolling is completed. Caching the passed position value with the corresponding View (or child thereof) is the correct way to keep track of it. The last position for which getView() is called isn't going to tell you anything relevant to how your AdapterView should behave, and you shouldn't rely on it for such.