Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As Romain Guy <a href="http://www.youtube.com/watch?v=wDBM6wVEO70&amp;t=45m46s" rel="noreferrer">explained a while back</a> during the <a href="http://www.google.com/events/io/2010/sessions/world-of-listview-android.html" rel="noreferrer">Google I/O session</a>, the most efficient way to only update one view in a list view is something like the following (this one update the whole view data):</p> <pre><code>ListView list = getListView(); int start = list.getFirstVisiblePosition(); for(int i=start, j=list.getLastVisiblePosition();i&lt;=j;i++) if(target==list.getItemAtPosition(i)){ View view = list.getChildAt(i-start); list.getAdapter().getView(i, view, list); break; } </code></pre> <p>Assuming <code>target</code> is one item of the adapter.</p> <p>This code retrieve the <code>ListView</code>, then browse the currently shown views, compare the <code>target</code> item you are looking for with each displayed view items, and if your target is among those, get the enclosing view and execute the adapter <code>getView</code> on that view to refresh the display.</p> <p>As a side note <code>invalidate</code> doesn't work like some people expect and will not refresh the view like getView does, <code>notifyDataSetChanged</code> will rebuild the whole list and end up calling <code>getview</code> for every displayed items and <code>invalidateViews</code> will also affect a bunch.</p> <p>One last thing, one can also get extra performance if he only needs to change a child of a row view and not the whole row like <code>getView</code> does. In that case, the following code can replace <code>list.getAdapter().getView(i, view, list);</code> (example to change a <code>TextView</code> text):</p> <pre><code>((TextView)view.findViewById(R.id.myid)).setText("some new text"); </code></pre> <p>In code we trust.</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