Monday, May 9, 2011

Using convertView and returning the correct view in Adapter's getView in one line!

Are you tired seeing or typing these lines over and over again?

public View getView(int position, View convertView, ViewGroup parent) {
    View v;
    if (convertView == null) {
        v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.row, null);
    } else {
        v = convertView;
    }
    // later:
    return v;
}

Do that in one line!

View v = convertView != null? convertView: LayoutInflater.from(getApplicationContext()).inflate(R.layout.row, null);

This seems easy but at first I didn't think of that!

No comments:

Post a Comment