Note that there are some explanatory texts on larger screens.

plurals
  1. POgetView called with wrong position when scrolling fast
    primarykey
    data
    text
    <p>fairly new Android developer here.</p> <p>I've come across a strange problem that I'm not sure how to work around. I've read a lot of problems around here that sound like they are the same problem I am having, but the solutions to their problems never seemed applicable to what is going on here.</p> <p>I have a custom listView set up using a separate class that extends BaseAdapter and uses a view holder (I set this up looking at various different examples of custom listviews). It's got a text view, a button, a progress bar, and an image button, and these items are hidden/changed based on an AsyncTask used to download a file.</p> <p>Everything works fine when I scroll through the list slowly. When I scroll the list rapidly up/down, it is as if the views of some cells in the list get re-assigned to other cells.</p> <p>I put this line into the top of my "getView" method in my adapter class:</p> <pre><code> @Override public View getView(final int pos, View convertView, ViewGroup parent) { Log.i("ks3","getView called, position is " + pos); </code></pre> <p>I have 7 items in my list, and 6 of them fit on screen. When it first gets displayed, I see this in my log:</p> <pre><code>getView called, position is 0 getView called, position is 1 getView called, position is 2 getView called, position is 3 getView called, position is 4 getView called, position is 5 </code></pre> <p>When I scroll down slowly to the next item, this is printed out next:</p> <pre><code>getView called, position is 6 </code></pre> <p>When I scroll back up, this:</p> <pre><code>getView called, position is 0 </code></pre> <p>Going down and up slowly produce these results flawlessly.</p> <p>When I start scrolling back and forth quickly, it prints out this message a bunch of times, mostly showing 6 and 0 as the position, but occasionally I will see these as well:</p> <pre><code>getView called, position is 1 getView called, position is 5 </code></pre> <p>Positions 1 and 5 should not be getting called, as they are always on-screen. It is as if getView got all jumbled up. At the same time, as I stated before, my list will look strange. Image buttons will be moved up or down a cell where they should not be.</p> <p>If I go back to scrolling smoothly, I see only 0's and 6's for position again.</p> <p>I honestly am not sure how to work around this. I thought maybe I could limit how fast you are able to scroll through the list, but haven't been able to find anything that works.</p> <p>Thanks!</p> <p>Edit: I wanted to update a couple things regarding this question. The first being that, from a comment here and from the Google video on listView, it has come to my attention that getView can be called for other things then what I imagined it was for, such as measurements, and so I should not be alarmed by what I originally thought was part of my problem (that being that I thought getView was being called with the wrong positions).</p> <p>Secondly, I saw multiple times that it is a very bad idea to "cache views inside your adapter." I am not exactly clear on what this means, but I'm pretty sure it is one of the things I am doing wrong (I save an instance of a button, progressBar, imageButton...).</p> <p>That, along with the fact that I update the views outside of getView and I don't use notifyDataSetChanged(); those together are probably causing some wonky stuff to happen.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload