How does ArrayAdapter getView() method works?

后端 未结 2 1110
庸人自扰
庸人自扰 2021-02-02 07:03

I want to do an ArrayAdapter to display an image and text. I don\'t want examples if possible. I\'d like someone to explain me how getView() works.

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-02 07:32

    in BaseAdapter you have getView function that is called by for an AdapterView i.e. ListView.

    you need to override getCount method of the BaseAdapter to return total number of views to diplay.

    And in getView you get following things:

    public View getView(int position, View convertView, ViewGroup parent) 
    
    1. position:

      getView going to be called for each position every time it is displayed.

    2. convertView

      As getView is call many times inflating a new view every time is expensive so list view provides you one of the previously created view to re-use.

    3. parent

      A reference to the parent view that this view will be a child of.

    ArrayAdapter is a subclass of BaseAdapter which takes ArrayList(or array) in constructor. And overrides getCount for you.

    So all you need to implement is getView

提交回复
热议问题