Note that there are some explanatory texts on larger screens.

plurals
  1. POYet another getView called multiple times
    text
    copied!<p>Each item in my <code>ListView</code> containt an <code>ImageView</code> and a <code>TextView</code>, this will be filled with remote information.</p> <p>I got an URL for the <code>ImageView</code>, therefore I start an <code>AsyncTask</code> which downloads the image and will call <code>setImageBitmap</code> with the downloaded bitmap.</p> <p>This goes very well but when the ListView is created, the <code>getView()</code> is called to often. It calls about 7 times the <code>getView</code> for the first 10 rows (only 7 visible). (So: 0, 1, etc, 10, 0, 1 etc).</p> <p>Then I can just scroll smoothly to the 10th item. But after that, for each new row the listview calls again about 7 times the <code>getView</code> for the first 10 items. (This will cause lag..)</p> <p>But when I remove the <code>setImageBitmap</code> from the <code>AsyncTask</code>, this all won't happen!</p> <p>What could be the problem? Could it be that some layout is to big which will cause another streak of getViews ?</p> <p>Here some code:</p> <pre><code>&lt;ListView android:id="@+id/mylist" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentBottom="true" android:layout_below="@+id/mydivider" android:divider="@+color/mycolor" android:dividerHeight="2dp" android:fadingEdge="none" android:fastScrollEnabled="true" android:listSelector="@android:color/transparent" android:scrollbars="horizontal" android:scrollingCache="false" android:smoothScrollbar="false" /&gt; </code></pre> <p>The AsyncTask:</p> <pre><code>public static class MyTask extends AsyncTask&lt;String, Integer, Bitmap&gt; { private LruCache&lt;String, Bitmap&gt; mMap; private String mUri; private int mPosition; private ViewHolder mHolder; public MyTask (ViewHolder holder, int position, LruCache&lt;String, Bitmap&gt; map) { mMap = map; mHolder = holder; mPosition = position; } @Override protected Bitmap doInBackground(String... url) { mUri = url[0]; return getBitmapFromURL(mUri); } @Override protected void onPostExecute(Bitmap b) { if (b != null) { mMap.put(mUri, b); if (mPosition == mHolder.position) { holder.image.setImageBitmap(b); } } }; } </code></pre> <p>getView()</p> <pre><code>row = convertView; ViewHolder holder; if (row == null) { row = vi.inflate(R.layout.mylayout, null); holder = new ViewHolder(); holder.image = (ImageView) row.findViewById(R.id.myimage); holder.title = (TextView) row.findViewById(R.id.mytext); row.setTag(holder); } else { holder = (ViewHolder) row.getTag(); } holder.title.setText("text"); holder.image.setImageResource(R.drawable.mydefaulticon); holder.position = position; startMyTask(content, holder, position); </code></pre> <p>Some more information:</p> <p>When an new view is created the stacktrace shown the getView was called from <code>ListView.makeAndAddView()</code> But in the useless streak of getViews it is coming from <code>ListView.measureHeigthOfChildren()</code></p> <p>So it seems like the layout is changed when I set the Bitmap...</p>
 

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