I am writing a custom adapter for ListView which extends BaseAdapter and in this method
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.x, null);
}
why is the check if(vi==null)
performed. getView()
is called for row when it comes in the visible area of the screen. So vi
will be null
everytime getview()
is called? So why is the check necessary here.?
P.S. I was getting some problem on coloring some specific rows of the listview, but when i removed this check, everything works fine. Thats why i am wondering over its usefullness.
The convertView
parameter may be a recycled view (for example, after scrolling down, the top rows become invisible, so their View
objects are not destroyed, but recycled and passed as parameters for reuse).
However, the very first time a draw request comes, there is no view (e.g. the first time the screen with the list is loaded). Hence, in this case convertView
has no value because nothing has been recycled (it is null
), in which case you must create the rows using the inflater.
convertView : is for recycling. Let's say you have a listview which can only display 10 item at a time, and currently it is displaying item 1 -> item 10. When you scroll down one item, the item 1 will be out of screen, and item 11 will be displayed. To generate View for item 11, the getView() method will be call, and convertView here is the view of item 1 (which is not neccessary anymore). So instead create a new View object for item 11 (which is costly), why not re-use convertView? => we just check convertView is null or not, if null create new view, else re-use convertView.