Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ArrayAdapter getView() method works?

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.

Thanks.

like image 702
Alex Moreno Avatar asked Jun 22 '11 15:06

Alex Moreno


People also ask

What does an ArrayAdapter do?

ArrayAdapter is an Android SDK class for adapting an array of objects as a datasource. Adapters are used by Android to treat a result set uniformly whether it's from a database, file, or in-memory objects so that it can be displayed in a UI element. The ArrayAdapter is useful for the latter.

What is ArrayAdapter in Kotlin?

android.widget.ArrayAdapter. You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .

What is the difference between BaseAdapter and ArrayAdapter?

Here is the difference: BaseAdapter is a very generic adapter that allows you to do pretty much whatever you want. However, you have to do a bit more coding yourself to get it working. ArrayAdapter is a more complete implementation that works well for data in arrays or ArrayList s.

What is the use of setAdapter in Android?

A list view is an adapter view that does not know the details, such as type and contents, of the views it contains. Instead list view requests views on demand from a ListAdapter as needed, such as to display new views as the user scrolls up or down. In order to display items in the list, call setAdapter(android.


2 Answers

getView() is the main part of your adapter. It returns View that will be displayed as your list/grid/gallary/any view that use adapter item. It triggers when you scroll the view(list for example).

So the first thing you should do its to create your custom adapter. You may extend it from BaseAdapter. Then you need to create some data to display (or pass it to adapter from out side - its better solution).

After that override getView() method and make sure to return your custom View there. In your case it should be a Layout with ImageView and TextView (and dont forget to fill them).

You can learn more from :

  • http://www.youtube.com/watch?v=N6YdwzAvwOA
  • http://www.edureka.in/blog/what-are-adapters-in-android/
  • http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/
like image 81
Yakiv Mospan Avatar answered Sep 21 '22 15:09

Yakiv Mospan


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

like image 26
vipul mittal Avatar answered Sep 22 '22 15:09

vipul mittal