Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overriding Adapter.getView

I'm new to android programming and doing the first steps with Adapters (for a ListView).

Overriding the Adapter.getView I often see things like this:

public View getView(int position, View convertView, ViewGroup parent) {

    View itemView = null;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) parent.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemView = inflater.inflate(R.layout.table_row, null);
    } else {
        itemView = convertView;
    }

    // play with itemView

    return itemView;
}

My question is what speaks against this:

public View getView(int position, View convertView, ViewGroup parent) {

    View itemView = super(position, convertView, parent);

    // play with itemView

    return itemView;
}

This seems to work for me but I'm sure there's a big point I'm missing :D

Thanks for reading and sorry for my bad english ...

like image 712
made Avatar asked Sep 15 '12 12:09

made


3 Answers

You can use

View itemView = super(position, convertView, parent);

if only you are deriving from "ready to use" adapters (not BaseAdapter), like SimpleAdapter, or ArrayAdapter, as they already have their implementation for the getView().

Have a look at them: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r2.1/android/widget/ArrayAdapter.java#361 for the ArrayAdapter, and http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r2.1/android/widget/SimpleAdapter.java#113 for SimpleAdapter.

If you derive from BaseAdapter, you will have to manualy implement the whole method, as you've described in the first example, because it does not have it out of the box: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r2.1/android/widget/BaseAdapter.java#BaseAdapter

like image 77
Vasily Sochinsky Avatar answered Sep 23 '22 09:09

Vasily Sochinsky


The getView(..)-method of the Adapter can be multiple ways. The only question is, which one is the most efficient?

An interesting article to read and make you understand the ListView more detailed: http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/

like image 20
DroidBender Avatar answered Sep 20 '22 09:09

DroidBender


If you mean that this piece of code:

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) parent.getContext()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    itemView = inflater.inflate(R.layout.table_row, null);
} else {
    itemView = convertView;
}

seems unnecessary for you: this piece of code allows Android to create a relatively small number of cells (equals to the number of cells that are visible on your screen +-), and then "recycle" these cells - use them over and over again while the user scrolls the list, instead of creating a cell for each object in your array. This will help you with:

  1. Saving memory - because you don't create view for each element in your array

  2. Saving CPU usage - creating a view object out of xml file ("inflating") is relatively expensive task and doing so for each item in your array might choke your UI thread

like image 22
ofirbt Avatar answered Sep 20 '22 09:09

ofirbt