Skip to content

Instantly share code, notes, and snippets.

Created January 10, 2013 09:32
Show Gist options
  • Save anonymous/4500760 to your computer and use it in GitHub Desktop.
Save anonymous/4500760 to your computer and use it in GitHub Desktop.
Useful code for updating certain row in ListView when developing Android application.
/**
* Update single item inside {@link ListView}.
* @param target {@link Object} object to update view.
*/
private void refreshListItem(Object target){
ListView list = getListView();
// Find position of the first visible row
int start = list.getFirstVisiblePosition();
// Find position of the last visible row
int end = list.getLastVisiblePosition();
for(int i=start, j=end;i<=j;i++) {
if (target == list.getItemAtPosition(i)) {
// Target view to process
View view = list.getChildAt(i - start);
// Calling getView will redraw the whole item
View v = list.getAdapter().getView(i, view, list);
// Call requestLayout to update only certain item in the view
view.findViewById(R.id.image).requestLayout();
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment