Monday, April 25, 2011

Better findViewById that doesn't require you to type-cast the result

If you started developing Android apps, you must have known the findViewById method. That is the method that glues UI and code.

However, findViewById returns the generic View, so you need to type-cast it to the specific view if you want to do widget-specific operations on it (e.g. setText).

TextView lAddress = (TextView) findViewById(R.id.lAddress);

or,

ImageButton bSubmit = (ImageButton) findViewById(R.id.bSubmit);

That's so annoying, even more if you change the type of the widget with similar but unrelated ones, like swapping a Button with an ImageButton.

Solution: use a generic method that returns you an object of the type that you request.

Paste the 2 methods below anywhere you like.

/**
 * Convenience method of findViewById
 */
@SuppressWarnings("unchecked")
public static <T extends View> T getView(View parent, int id) {
 return (T) parent.findViewById(id);
}

/**
 * Convenience method of findViewById
 */
@SuppressWarnings("unchecked")
public static <T extends View> T getView(Activity activity, int id) {
 return (T) activity.findViewById(id);
}

Now, you can just call them like:

TextView lAddress = getView(this, R.id.lAddress);

// or, if not inside an activity, let's say in an Adapter's getView:
View res = convertView != null? convertView: getLayoutInflater().inflate(R.layout.item_person, null);
TextView lAddress = getView(res, R.id.lAddress);

No comments:

Post a Comment