2

I'am trying to use a custom listView in my application and i have some questions about its working principles.When i implemented a custom adapter,how its methods(especially the getView method)work without calling them from any other class ?

Tartar
  • 5,149
  • 16
  • 63
  • 104

3 Answers3

1

When you set the adapter to a view (e.g. ListView or GridView), that view at some point wants to have some items to show. So it calls getView in the adapter:

getView(int position, View convertView, ViewGroup parent).

The position is the position of the item in the list/grid/etc. The convertView is a recycled view that can be already inflated by a previous getView() call, or null when it's not inflated yet (see this answer about convertView and view recycling.

The parent is used to inflated the view, so the correct layout parameters can be computed in relation to the parent view.

Note that inflation is slow. That is why the convertView mechanism exists, to recycle views so the number of inflations is minimized. Next to that, finding views (findViewById()) is also relatively slow. To improve on that, check out the ViewHolder pattern, which keeps references to views in memory so they don't have to be searched for each time.

Community
  • 1
  • 1
Jeffrey Klardie
  • 3,020
  • 1
  • 18
  • 23
0

I think this link can help you. getview is a callback function which will be called automatically when you will display your listview on Activity. When you display your listview then you overrides getview and inflates your row from XML or dynamically creates your row. That row you return as a view which displays in your listview.

How does the getView() method work when creating your own custom adapter?

For each row getview will be called once. You create your layouts and return them as view. Those respective views displays in your lisview rows.

Community
  • 1
  • 1
Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
0

You are calling the custom adapter class from you activity class.Your customised adapter class extends a BaseAdapter which is an abstract class.The methods of an abstract will be used by the extended class(the methods like getView(), getItemId(), getItem(), and getCount()).These methods should not need a seperate call from your Class since you are calling the customised adapter classs .

Ansar
  • 364
  • 3
  • 16