2

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.

Diffy
  • 2,339
  • 3
  • 25
  • 47
  • This is used for avoid duplicate values in your adapter. The view is not a recycled one we have to inflate – Piyush Sep 29 '14 at 06:17
  • @PG_Android, is there a condition destroyView() which can be applied when the row is destroyed? Because with the null condition on, if i color 5th row of the list, i also get the color in 10th row. – Diffy Sep 29 '14 at 06:24
  • 1
    @Diffy What happens when you remove the `null` condition is that you **always** inflate a new layout, even though a recycled one is available. What you *should* do when `vi` is not `null` is reset its attributes and specifically set new ones for the row. In other words, never leave a recycled view as it is. – Bogdan Alexandru Sep 29 '14 at 06:28
  • @BogdanAlexandru, Ohh yes i can reset its attributes if its not null. Thanks. – Diffy Sep 29 '14 at 06:31
  • Take out some time and read this http://stackoverflow.com/a/14108676/1939564 – Muhammad Babar Sep 29 '14 at 06:32
  • Okay i will @MuhammadBabar. Thats a nice link. – Diffy Sep 29 '14 at 06:46

2 Answers2

4

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.

Bogdan Alexandru
  • 5,394
  • 6
  • 34
  • 54
  • Okay.Makes sense. This also solves why i was getting problem in coloring the rows. – Diffy Sep 29 '14 at 06:16
  • Ok, I'm glad it helped :) When I first started Android I got scared of the custom adapters. However, once they start making sense, they become an extremely powerful weapon in your arsenal. – Bogdan Alexandru Sep 29 '14 at 06:19
2

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.

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • Is there a condition destroyView() which can be applied when the row is destroyed? Because with the null condition on, if i color 5th row of the list, i also get the color in 10th row because i think the same row is recycled? – Diffy Sep 29 '14 at 06:25
  • no there is no destroyView(),in adapter you have to maintain item data positioning wise. – Haresh Chhelana Sep 29 '14 at 06:27